問題描述
搜索了好幾個小時后,我開始認為這是不可能的.
After searching for many hours I am starting to think this is impossible.
我需要為每次運行使用不同的經過身份驗證的(非公共)代理通過 selenium 運行 Chrome.
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 將啟動并顯示錯誤:
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.
我還嘗試了在用戶 ID 前添加 http://
的變體:
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 身份驗證代理
不確定 browswermob-proxy
是什么或應該做什么或如何在 Python 中實現和測試.除非絕對必要,否則我討厭堆積創可貼解決方案.
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.
編輯(21 年 11 月 8 日):
EDIT (08NOV21):
我已經多年不使用 Selenium 了.正因為如此,我現在缺乏上下文(和時間,對不起)來檢查提供的較新答案并將一個標記為該問題的解決方案.SO 是否有一種機制可以用來有效地將這一職能委托給可能是該領域專業知識的當前從業者?
I have been away from using Selenium for many years. Because of this I now lack the context (and time, sorry) to go through the newer answers being provided and mark one as the solution to this problem. Does SO have a mechanism one could use to effectively delegate this function to someone who might be a current practitioner with expertise in this domain?
推薦答案
要在 python selenium 中使用帶有身份驗證的代理,您可以使用 硒線.
To use proxies with auth in python selenium you can use seleniumwire.
首先,使用 pip install selenium-wire
然后從 seleniumwire 導入 webdriver 而不是 selenium
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)
現在您可以像使用 selenium 一樣使用瀏覽器實例:browser.get('https://api.ipify.org')
等等...
Now you can use your browser instance exact the same way as selenium: browser.get('https://api.ipify.org')
and so on...
這篇關于如何在 selenium chromedriver 中使用經過身份驗證的代理?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!