2

How is it possible to change the proxy server in Selenium after starting the driver? I saw several threads on this topic, but none of the answers were correct. You can use not only Chrome but also Firefox.

If you have any ideas on how to change the proxy when the driver is open, please write.

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.proxy import Proxy, ProxyType
from selenium.webdriver.support.ui import WebDriverWait
from webdriver_manager.chrome import ChromeDriverManager
from selenium.webdriver.support import expected_conditions as EC
from selenium.common.exceptions import TimeoutException

Path = ChromeDriverManager().install()

options = webdriver.ChromeOptions()
options.add_argument('--no-sandbox')
options.add_argument('--disable-setuid-sandbox')
options.add_argument('--disable-dev-shm-usage')
options.add_argument('--window-size=600,400')
options.add_argument('--ignore-certificate-errors')
options.add_argument('--disable-accelerated-2d-canvas')
# options.add_argument('--no-zygote')
# options.add_argument('--single-process')
options.add_argument('--disable-gpu')
options.add_argument('--headless')

proxy_file = open("proxy.txt", "r", encoding="utf-8", errors="ignore").readlines()
proxy = ((random.choice(proxy_file)).replace("\n", ""))
options.add_argument('--proxy-server=%s' % proxy)
browser = webdriver.Chrome(Path, options=options)

browser.get('https://google.com')
# Here the proxy should change
browser.get('https://google.com')
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
Leo
  • 43
  • 9
  • maybe you should use own local proxy server which can redirect to other random proxy - and then you could connect Selenium to your local proxy server and you could control it and change random proxy. – furas Mar 22 '22 at 21:42
  • @furas I also thought about it, do you know how it can be implemented on ubuntu? – Leo Mar 22 '22 at 22:03
  • I never used local proxy server - only `Tor Network` installed with [Tor Browser](https://www.torproject.org/) and which can be controled by `socket` or using [stem](https://pypi.org/project/stem/) ([Python: How to use Tor Network with requests to change IP?](https://blog.furas.pl/python-how-to-use-tor-network-with-requests-to-change-ip.html) – furas Mar 22 '22 at 23:00
  • Does this answer your question? [How to dynamically change proxies in chrome driver](https://stackoverflow.com/questions/54911913/how-to-dynamically-change-proxies-in-chrome-driver) – kaliiiiiiiii Jun 28 '23 at 16:14

1 Answers1

2

No, you won't be able to change the proxy server using Selenium after starting the driver and the Browsing Context.

When you configure an instance of a ChromeDriver with ChromeOptions() to span a new Chrome Browsing Context the configuration gets baked within the chromedriver executable which will persist for the lifetime of the WebDriver and being uneditable. So you can't modify/add any existing/new configuration through ChromeOptions() class to the WebDriver instance which is currently in execution.

Even if you are able to extract the ChromeDriver and ChromeSession attributes e.g. Session ID, Cookies, UserAgent and other session attributes from the already initiated ChromeDriver and Chrome Browsing Session still you won't be able to change the set of attributes of the ChromeDriver.

A cleaner way would be to quit() the existing ChromeDriver and Chrome Browser instances gracefully and then span a new set of ChromeDriver and Chrome Browser instance with the new set of proxy configuration as follows:

options = webdriver.ChromeOptions()
options.add_argument('--no-sandbox')
options.add_argument('--disable-setuid-sandbox')
options.add_argument('--disable-dev-shm-usage')
options.add_argument('--window-size=600,400')
options.add_argument('--ignore-certificate-errors')
options.add_argument('--disable-accelerated-2d-canvas')
options.add_argument('--disable-gpu')
options.add_argument('--headless')
urls_to_visit = ['https://www.google.com/', 'https://stackoverflow.com/']
proxies = open("proxy.txt", "r", encoding="utf-8", errors="ignore").readlines()
for i in range(0, len(urls_to_visit)):
    proxy = ((random.choice(proxies)).replace("\n", ""))
    options.add_argument('--proxy-server=%s' % proxy)
    browser = webdriver.Chrome(Path, options=options)
    browser.get("{}".format(urls_to_visit[i]))
    # perform the tasks
    driver.quit()

References

You can find a couple of relevant discussions in:

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352