Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Selenium (Python) - Changing Proxy During Runitime?

I managed to use a proxy server with selenium for chrome using the code below:

chromedriver = "C:/Seltests/chromedriver"
os.environ["webdriver.chrome.driver"] = chromedriver
chrome_options = webdriver.ChromeOptions()
chrome_options.add_argument('--proxy-server=141.0.175.141:443')
driver = webdriver.Chrome(chrome_options=chrome_options)

However, I would like to know if it is possible to change that proxy to a new one during run-time. Or if there is any other way of doing this so it allows me to. I'm thinking that using the code above I would have to have the browser close then re-open to start a new session and use another proxy? Please help :)

like image 737
user3244437 Avatar asked Nov 01 '22 06:11

user3244437


1 Answers

You will have to re-launch the browser instance in order to achieve this. Wherever you want to change the proxy insert the following code:

driver.quit()
chrome_options = webdriver.ChromeOptions()
chrome_options.add_argument('--proxy-server=<new proxy>')
driver = webdriver.Chrome(chrome_options=chrome_options)

This will close the current browser and launch a new one with the new proxy.

like image 142
Vikas Ojha Avatar answered Nov 15 '22 04:11

Vikas Ojha