How to use authenticated proxy in selenium chromedriver?(如何在 selenium chromedriver 中使用经过身份验证的代理?)
问题描述
搜索了好几个小时后,我开始认为这是不可能的.
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 中使用经过身份验证的代理?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何在 selenium chromedriver 中使用经过身份验证的代理?


基础教程推荐
- Dask.array.套用_沿_轴:由于额外的元素([1]),使用dask.array的每一行作为另一个函数的输入失败 2022-01-01
- 何时使用 os.name、sys.platform 或 platform.system? 2022-01-01
- 在 Python 中,如果我在一个“with"中返回.块,文件还会关闭吗? 2022-01-01
- 用于分类数据的跳跃记号标签 2022-01-01
- 筛选NumPy数组 2022-01-01
- 如何让 python 脚本监听来自另一个脚本的输入 2022-01-01
- 使用PyInstaller后在Windows中打开可执行文件时出错 2022-01-01
- 线程时出现 msgbox 错误,GUI 块 2022-01-01
- Python kivy 入口点 inflateRest2 无法定位 libpng16-16.dll 2022-01-01
- 如何在海运重新绘制中自定义标题和y标签 2022-01-01