如何使用selenium和python在无头chorme浏览器中使用带身份验证的代理?

2024-04-27 05:39:59 发布

您现在位置:Python中文网/ 问答频道 /正文

我使用了期望的能力

capabilities = dict(DesiredCapabilities.CHROME)
capabilities['proxy'] = {'proxyType': 'MANUAL',
                         'httpProxy': proxy['address'],
                         'ftpProxy': proxy['address'],
                         'sslProxy': proxy['address'],
                         'noProxy': '',
                         'class': "org.openqa.selenium.Proxy",
                         'autodetect': False
                         }
chrome_options = Options()
chrome_options.add_argument('--headless')

但是什么也没有发生,如果我删除headless,我会看到需要身份验证的警报。在

我也用过

^{pr2}$

但这没用。我把“http”改成了“socks”、“socks4”和“socks5”,但这根本没用。在


Tags: address能力chromemanualdictproxyoptionsheadless
1条回答
网友
1楼 · 发布于 2024-04-27 05:39:59

不幸的是,headless chrome会丢弃警报(Issue 718235),并且不支持扩展(Issue 706008),因此我们不能使用在GUI模式下工作的技巧。这给我们留下了两个主要的选择:

选项1-代理重定向

运行一个不带身份验证的单独代理,并将其重定向到具有身份验证的目标代理。在

您可以用mitmproxy在一行中完成:
mitmproxy mode upstream:http://<target-proxy-ip>:<target-proxy-port> upstream-auth <user>:<password> -p 3128并配置您的客户机使用localhost:3128作为代理。在

You can install mitmproxy via pip: pip install mitmproxy (requires python3)
Reference:https://docs.mitmproxy.org/stable/overview-installation/

或者使用Squid代理:Squid: forward to another proxy (with authentication details for the parent proxy)

选项2-使用Xvfb显示服务器

Xvfb performs all graphical operations in virtual memory without showing any screen output
Reference:https://www.x.org/releases/X11R7.7/doc/man/man1/Xvfb.1.xhtml

以下是与Selenium一起使用的说明:How do I run Selenium in Xvfb?
Xvfb的Python包装器:https://github.com/ponty/pyvirtualdisplay

祝你好运!在

相关问题 更多 >