简体   繁体   中英

Selenium - Set http proxy to the webdriver

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

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/")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM