Changing the proxy server during Selenium

2019-03-02 18:08发布

So everything works

fp = webdriver.FirefoxProfile()
fp.set_preference("network.proxy.type", 1)
fp.set_preference("network.proxy.http", PROXY_HOST)
fp.set_preference("network.proxy.http_port", int(PROXY_PORT))
fp.update_preferences()
driver = webdriver.Firefox(firefox_profile=fp)

But if the driver has already been created, the proxy can not install. It does not work

driver = webdriver.Firefox()
driver.profile.set_preference("network.proxy.type", 1)
driver.profile.set_preference("network.proxy.http", PROXY_HOST)
driver.profile.set_preference("network.proxy.http_port", int(PROXY_PORT))
driver.profile.update_preferences()

And so, too.

 driver = webdriver.Firefox()
 driver.firefox_profile.set_preference("network.proxy.type", 1)
 driver.firefox_profile.set_preference("network.proxy.http", PROXY_HOST)
 driver.firefox_profile.set_preference("network.proxy.http_port", int(PROXY_PORT))
 driver.firefox_profile.update_preferences()

Why? Can not understand. I'm doing something wrong?

1条回答
Luminary・发光体
2楼-- · 2019-03-02 18:42

When using WebDriver with Firefox, the use of the profile is a one-time thing. When the driver launches the browser, it writes the profile object to disk, then starts the browser executable. After that point, there is no mechanism for the browser to read any further changes to the WebDriver profile object. To change the proxy, you have to set the settings in the profile before the browser is launched.

查看更多
登录 后发表回答