Search code examples
pythonseleniumselenium-webdriverproxies

Selenium Python Running a Browser with a Proxy


I am trying to write a very simple script that takes a proxy (that does not need authentication) from a txt file and opens a browser with it, and then loops this action for a certain amount of times going down the proxy list. I do know how to open a txt file and use it, my main problem is getting the proxies to work. I have seen similar questions asked and I have taken the solutions and tried them, and I can get them to run without errors but the browser opens and there is no proxy. I am sure this is a very simple task, but I am very new to python and I have not gotten it to work. Thanks!

This is what I used; there are no errors but when the browser opens in IP chicken it shows my IP, not the proxy (this is just for 1 browser not the loop I was talking about):

from selenium import webdriver
from selenium.webdriver.common.proxy import Proxy, ProxyType

options = webdriver.ChromeOptions()

proxy = Proxy()
proxy.proxyType = ProxyType.MANUAL
proxy.autodetect = False
proxy.httpProxy = proxy.sslProxy = proxy.socksProxy = "96.70.52.227:48324"
options.Proxy = proxy
options.add_argument("ignore-certificate-errors")


driver = webdriver.Chrome('/Users/aiden/Downloads/chromedriver', options=options)
driver.get('https://www.ipchicken.com/')

Solution

  • Try below solution:

    from selenium import webdriver
    
    PROXY = "96.70.52.227:48324" #  HOST:PORT
    
    chrome_options = webdriver.ChromeOptions()
    chrome_options.add_argument('--proxy-server=%s' % PROXY)
    chrome_options.add_argument("ignore-certificate-errors")
    
    chrome = webdriver.Chrome(options=chrome_options)
    chrome.get("https://www.ipchicken.com/")