Selenium – Set http proxy to the webdriver

Issue

I’ve been trying to use a http proxy with Selenium.
What I have so far is:

PROXY = "46.4.29.2:80"
webdriver.DesiredCapabilities.CHROME['proxy'] = {
    "httpProxy": PROXY,
    "ftpProxy": PROXY,
    "sslProxy": PROXY,
    "noProxy": None,
    "proxyType": "MANUAL",
    "class": "org.openqa.selenium.Proxy",
    "autodetect": False
}

However, it simply doesn’t work and throws:

selenium.common.exceptions.WebDriverException: Message: unknown error: net::ERR_TUNNEL_CONNECTION_FAILED

Solution

Personally, I use a library called selenium-wire https://pypi.org/project/selenium-wire/ which can be used for both authenticated and non-authenticated proxies. Here is the documentation reference for proxy use https://github.com/wkeeling/selenium-wire#proxies

I have made a short snippet of code that uses a public proxy to go to google.com

import seleniumwire
from seleniumwire import webdriver

options = {'proxy': 
{
    'http' : 'http://45.152.188.246:3128'}
}

driver = webdriver.Chrome(seleniumwire_options=options)

driver.get("https://google.com/")

Here is also a method that would work without the need for selenium-wire.

options = webdriver.ChromeOptions()
options.add_argument('--proxy-server=45.152.188.246:3128')

driver = webdriver.Chrome(options=options)

driver.get("https://google.com/")

Answered By – Akdeniz

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published