连接到命令行生成的 google-chrome 时如何使用 Selenium 选项设置代理

问题描述 投票:0回答:1

我使用以下命令成功从终端运行 Chrome 浏览器:

google-chrome --remote-debugging-port=9222 --user-data-dir="C:\selenum\ChromeProfile"

然后,我可以在初始化驱动程序时使用以下代码行连接到此浏览器实例:

options.add_experimental_option("debuggerAddress", "127.0.0.1:9222")

我现在想设置一个代理服务器地址,格式为“http://用户名:密码@proxyhost:端口”。

我通常能够通过使用manifest_json块并添加到选项中来很好地使用Selenium代理,即:

pluginfile = 'proxy_auth_plugin.zip'
with zipfile.ZipFile(pluginfile, 'w') as zp:
    zp.writestr("manifest.json", manifest_json)
    zp.writestr("background.js", background_js)
options.add_extension(pluginfile)

但是,当将上述实验性调试器选项(上面)添加到代码中时,代理将不再工作并使用系统 IP 地址。

我还尝试使用'--proxy-server =“http://username:password@proxyhost:port”从命令行设置代理,但据我所知,Chrome不会重音用户名和密码通过命令行。

所以我想知道在连接到命令行生成的 Chrome 实例后是否有任何方法可以实现使用 Selenium 设置代理?

预先感谢您的帮助。

python selenium-webdriver http-proxy google-chrome-headless
1个回答
0
投票

当您从命令行启动 Chrome 时,您需要指定一个用于设置经过身份验证的代理设置的扩展程序。要创建该扩展,请使用 https://stackoverflow.com/a/35293284/7058266 中提供的信息。

要在启动 Chrome 时指定扩展程序,请使用

--load-extension=path/to/extension
(https://stackoverflow.com/a/22198816/7058266)。在你的情况下,可能看起来像这样:

google-chrome --remote-debugging-port=9222 --load-extension=path/to/extension

有一个名为 SeleniumBase 的 Python 框架已经可以完成这些事情。这是创建 proxy-with-auth 扩展的 Python 代码。在将 chromedriver 附加到 Chrome 之前启动 Chrome 的 SeleniumBase UC 模式代码可以在此处找到。 (用于逃避机器人检测服务。)

© www.soinside.com 2019 - 2024. All rights reserved.