Allexj
Allexj

Reputation: 1487

Python - How to use selenium with proxies

driver = webdriver.Firefox()
for x in range(10):
    driver.get("mysite.com")

Is there a way to change the proxy on every connection to "mysite.com" in the range 10, but without closing the driver and reopening it, but just changing the settings of proxy?

Upvotes: 2

Views: 2289

Answers (1)

Shubham Jain
Shubham Jain

Reputation: 17553

You need to import the following:

from selenium.webdriver.common.proxy import *

Then setup the proxies:

myProxy = "xx.xx.xx.xx:xxxx"

proxy = Proxy({
    'proxyType': ProxyType.MANUAL,
    'httpProxy': myProxy,
    'ftpProxy': myProxy,
    'sslProxy': myProxy,
    'noProxy': '' # set this value as desired
    })

Then call the webdriver.Firefox() function as follows:

driver = webdriver.Firefox(proxy=proxy)
driver.get("http://www.google.com")

Or you can use tor browser it will switch the proxy automatically

Upvotes: 5

Related Questions