【问题标题】:PhantomJS Proxy when using Remote Webdriver?使用远程 Webdriver 时的 PhantomJS 代理?
【发布时间】:2015-08-24 21:42:08
【问题描述】:

我正在尝试在带有 PhantomJS 的 python 中使用 selenium。我正在运行一个 selenium 集线器服务器,所以我使用 webdriver.Remote 来启动一个 webdriver。

将代理传递给 PhantomJS 的正常方式是:

service_args = [
    '--proxy=127.0.0.1:9999',
    '--proxy-type=socks5',
    ]
browser = webdriver.PhantomJS('../path_to/phantomjs',service_args=service_args)

这对

不起作用
webdriver.Remote(service_args=service_args)

因为 webdriver.Remote 只接受desired_capabilities,而不是服务参数,作为参数。

有没有办法将代理传递给 PhantomJS 作为 desired_capibility?

使用 Firefox 网络驱动程序执行此操作的典型方法不起作用。

【问题讨论】:

    标签: python selenium-webdriver phantomjs ghostdriver remotewebdriver


    【解决方案1】:

    由于 PhantomJS 实例已经运行,将命令行选项传递给 RemoteDriver 构造函数是没有意义的。不过有办法。

    PhantomJS 本身支持通过phantom.setProxy(ip, port, type, un, pw) 配置代理的编程方式(未记录,但自 PhantomJS 2 起可用)。这必须在虚拟上下文中执行,所以driver.execute_script() 在这里不起作用。

    GhostDriver 接受这样的脚本,这些脚本将通过一个特殊命令在幻像上下文中执行,您可以像这样调用它 (source):

    driver.command_executor._commands['executePhantomScript'] = ('POST', '/session/$sessionId/phantom/execute')
    driver.execute('executePhantomScript', {'script': '''phantom.setProxy("10.0.0.1", 12345);''', 'args' : [] })
    

    【讨论】:

    • 嗯,这可能是在正确的轨道上,但我收到一个错误 - “未定义”不是 phantom.setProxy 的函数...
    • 我认为我的 Phantom 版本可能是问题,但我正在运行 PhantomJS 1.9.8 - 所以我在这方面应该没问题
    • phantom.setProxy() 自 PhantomJS 2 起可用。
    • 这不是线程安全的解决方案,因为它为整个 PhantomJS 实例设置代理,而不是每个 webdriver 会话。使用 desired_capabilities 设置代理会好得多...
    • @gwaramadze 如果您有更好的解决方案,总有可能发布另一个答案。
    猜你喜欢
    • 2016-12-05
    • 2014-07-23
    • 2013-06-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-01-19
    相关资源
    最近更新 更多