Dynamically changing proxy in Firefox with Selenium webdriver

Viewed 7437

Is there any way to dynamically change the proxy being used by Firefox when using selenium webdriver?

Currently I have proxy support using a proxy profile but is there a way to change the proxy when the browser is alive and running?

My current code:

proxy = Proxy({
    'proxyType': 'MANUAL',
    'httpProxy': proxy_ip,
    'ftpProxy': proxy_ip,
    'sslProxy': proxy_ip,
    'noProxy': '' # set this value as desired
    })
browser = webdriver.Firefox(proxy=proxy)

Thanks in advance.

4 Answers

Have a try selenium-wire, It can even override header field

from seleniumwire import webdriver 

options = {
    'proxy': {
          "http": "http://" + IP_PORT, 
          "https": "http://" + IP_PORT,
          'custom_authorization':AUTH
          },
    'connection_keep_alive': True,
    'connection_timeout': 30,
    'verify_ssl': False 
}

# Create a new instance of the Firefox driver
driver = webdriver.Firefox(seleniumwire_options=options)
driver.header_overrides = {
    'Proxy-Authorization': AUTH
}

# Go to the Google home page
driver.get("http://whatismyip.com")
driver.close()
Related