我正在尝试将特定配置文件加载到 selenium 中,但它始终以默认配置文件打开。这是我的代码:
profile_path = '/Users/mymachine/Library/Application Support/Firefox/Profiles/specificprofile.default'
options = webdriver.FirefoxOptions()
options.set_preference('profile', profile_path)
service = FirefoxService(GeckoDriverManager().install())
driver = webdriver.Firefox(service=service, options=options)
我也尝试过直接指示 gecko 驱动程序,但它也不起作用。
service = webdriver.FirefoxService(executable_path="/usr/local/bin/geckodriver")
当我记录输出时,我得到:
1695479350799 mozrunner::runner INFO Running command: MOZ_CRASHREPORTER="1" MOZ_CRASHREPORTER_NO_REPORT="1" MOZ_CRASHREPORTER_SHUTDOWN="1" MOZ_NO_REMOTE="1" "/App ... s" "localhost" "-foreground" "-no-remote" "-profile" "/var/folders/0k/hz9yngm56nz1htdc3c3t3d0c0000gn/T/rust_mozprofileecFA3Z"
console.warn: services.settings: Ignoring preference override of remote settings server
console.warn: services.settings: Allow by setting MOZ_REMOTE_SETTINGS_DEVTOOLS=1 in the environment
1695479351216 Marionette INFO Marionette enabled
console.error: "Warning: unrecognized command line flag" "-remote-allow-hosts"
1695479351323 Marionette INFO Listening on port 60997
Read port: 60997
WebDriver BiDi listening on ws://127.0.0.1:60987
1695479351366 RemoteAgent WARN TLS certificate errors will be ignored for this session
UNSUPPORTED (log once): POSSIBLE ISSUE: unit 1 GLD_TEXTURE_INDEX_2D is unloadable and bound to sampler type (Float) - using zero texture because texture unloadable
DevTools listening on ws://127.0.0.1:60987/devtools/browser/2bbd8a80-f4ee-46a3-875d-7be3c1b2d504
console.warn: services.settings: Ignoring preference override of remote settings server
console.warn: services.settings: Allow by setting MOZ_REMOTE_SETTINGS_DEVTOOLS=1 in the environment
console.error: (new TypeError("lazy.AsyncShutdown.profileBeforeChange is undefined", "resource://services-settings/Database.sys.mjs", 593))
其中
"-profile" "/var/folders/0k/hz9yngm56nz1htdc3c3t3d0c0000gn/T/rust_mozprofileecFA3Z"
引起了我的注意,表明它正在创建临时配置文件。
我使用的是 MacOS Darwin M1,使用 selenium 4.12.0、webdriver-manager 4.0.0。
建议您使用 bose 框架(selenium 框架)的配置文件功能
1️⃣ 安装魔法🧙u200d♀️:
python -m pip install botasaurus
mkdir my-botasaurus-project && cd my-botasaurus-project
2️⃣ 在
main.py
中编写代码以从 omkar.cloud 中抓取标题 👨u200d💻:
from bose.launch_tasks import launch_tasks
from bose import *
class Task(BaseTask):
browser_config = BrowserConfig(
profile='1',
)
def run(self, driver: BoseDriver, data):
driver.get("https://www.omkar.cloud/")
heading = driver.get_element_or_none_by_selector("h1")
heading_text = heading.text
return {
"heading": heading_text
}
if __name__ == "__main__":
launch_tasks(Task)
3️⃣开始抓取😎:
python main.py
注意:Bose 仅支持 Chrome,不支持 Firefox