Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.0k views
in Technique[技术] by (71.8m points)

python - How to use authenticated proxy in selenium chromedriver?

After searching for many hours I am starting to think this is impossible.

I need to run Chrome through selenium using different authenticated (not public) proxy's for each run.

PROXY_IP = "<some IP address>"
UID = "<the user id>"
PWD = "<the password">

options = webdriver.ChromeOptions()
options.add_argument("--proxy-server=%s:%s@%s" % (UID,PWD,PROXY_IP))

driver = webdriver.Chrome(executable_path=".\driver\chromedriver.exe",
                          chrome_options=options)
driver.get("<site URL>")

Chrome will fire-up and display the error:

This webpage is not available
ERR_NO_SUPPORTED_PROXIES

If I use a public proxy requiring no authentication like this...

PROXY_IP = "<public proxy IP address>"

options = webdriver.ChromeOptions()
options.add_argument("--proxy-server=%s" % PROXY_IP)

driver = webdriver.Chrome(executable_path=".\driver\chromedriver.exe",
                          chrome_options=options)
driver.get("<site URL>")

...it runs just fine and displays the site while using the proxy.

I also tried a variant with http:// in front of the user ID:

options.add_argument("--proxy-server=http://%s:%s@%s" % (UID,PWD,PROXY_IP))

The fact that I have searched far and wide and haven't found a solution leads me to believe none might exist.

I did find this but I can't make sense out of it:

selenium chromedriver authentication proxy

Not sure what browswermob-proxy is or is supposed to do or how to implement and test in Python. I hate piling up band-aid solutions unless they are absolutely necessary.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

To use proxies with auth in python selenium you can use seleniumwire.

Fistly, install it with pip install selenium-wire

Then import webdriver from seleniumwire instead selenium

from seleniumwire import webdriver
options = {
    'proxy': {
        'http': 'http://username:password@host:port', 
        'https': 'https://username:password@host:port',
        'no_proxy': 'localhost,127.0.0.1' # excludes
    }
}
browser = webdriver.Chrome(path_to_driver, seleniumwire_options=options)

Now you can use your browser instance exact the same way as selenium: browser.get('https://api.ipify.org') and so on...


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...