使用PhantomJS代理时使用远程Webdriver? [英] PhantomJS Proxy when using Remote Webdriver?

查看:201
本文介绍了使用PhantomJS代理时使用远程Webdriver?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正尝试在PhantomJS和python中使用硒.我正在运行硒中心服务器,因此正在使用webdriver.Remote启动Webdriver.

I am trying to use selenium in python with PhantomJS. I am running a selenium hub server so am using webdriver.Remote to start a webdriver.

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

The normal way to pass a proxy to PhantomJS is:

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仅将desirable_capabilities(而不是服务args)作为参数.

As webdriver.Remote takes only desired_capabilities, not service args, as a parameter.

是否有任何方法可以将proxy作为期望的功能传递给PhantomJS?

Is there any way to pass a proxy to PhantomJS as a desired_capibility?

使用Firefox Webdriver的典型方法不起作用.

The typical way one would do so with a Firefox webdriver does not work.

推荐答案

由于PhantomJS实例已经运行,因此将命令行选项传递给RemoteDriver构造函数没有任何意义.不过有办法.

Since the PhantomJS instance already runs, it wouldn't make sense to pass commandline options to the RemoteDriver constructor. There is a way though.

PhantomJS本身支持通过

PhantomJS itself supports a programmatic way to configure a proxy through phantom.setProxy(ip, port, type, un, pw) (not documented, but available since PhantomJS 2). This has to be executed in the phantom context, so driver.execute_script() won't work here.

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

GhostDriver accepts such script that are to be executed in the phantom context through a special command which you can invoke like this (source):

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

这篇关于使用PhantomJS代理时使用远程Webdriver?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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