Search code examples
pythonseleniumselenium-webdriverproxychrome-options

How to update the Proxy Server within the same session using Selenium and Python


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')

Solution

  • 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: