如何在 selenium chromedriver 中使用经过身份验证的代理? [英] How to use authenticated proxy in selenium chromedriver?

查看:44
本文介绍了如何在 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 中使用经过身份验证的代理?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆