如何在使用 Selenium 和 Python 定义 webdriver 后更改 firefox 配置文件首选项

How to change firefox profile preference after defining webdriver using Selenium and Python

如何在定义驱动程序后更改配置文件首选项?

profile = webdriver.FirefoxProfile()

driver = webdriver.Firefox(firefox_profile=profile)

部分代码后需要设置useragent

profile.set_preference("general.useragent.override", ua)

如何在不定义新驱动的情况下设置?

我认为这是不可能的,但我发现本文中解释了一些解决方法,但不确定这些方法是否可靠(或是否有效):https://tarunlalwani.com/post/change-profile-settings-at-runtime-firefox-selenium/

根据 once you configure the GeckoDriver with specific Capabilities and initialize the 会话的当前实施打开 浏览上下文 ,您无法更改功能运行时。即使您能够检索运行时功能,您仍然无法将它们更改回来。

因此,in-order要更改 Firefox 用户首选项,您必须启动一个新的 WebDriver 会话。

Note:However, you can change the for Firefox on each run and you can find a relevant discussion in


参考

这是与代理设置功能相关的 @JimEvans clear and concise comment(截至 2013 年 10 月 24 日 13:02):

When you set a proxy for any given driver, it is set only at the time WebDriver session is created; it cannot be changed at runtime. Even if you get the capabilities of the created session, you won't be able to change it. So the answer is, no, you must start a new session if you want to use different proxy settings.


结尾

您可以在以下位置找到一些相关的详细讨论: