简体   繁体   English

使用远程Webdriver时使用PhantomJS代理?

[英]PhantomJS Proxy when using Remote Webdriver?

I am trying to use selenium in python with PhantomJS. 我试图在PhantomJS中使用python中的硒。 I am running a selenium hub server so am using webdriver.Remote to start a webdriver. 我正在运行硒集线器服务器,因此正在使用webdriver.Remote启动Webdriver。

The normal way to pass a proxy to PhantomJS is: 将代理传递给PhantomJS的正常方法是:

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

This won't workthough for 这将无法工作

webdriver.Remote(service_args=service_args)

As webdriver.Remote takes only desired_capabilities, not service args, as a parameter. 作为webdriver.Remote,仅将required_capabilities而不是服务args作为参数。

Is there any way to pass a proxy to PhantomJS as a desired_capibility? 有什么方法可以将proxy作为期望的功能传递给PhantomJS吗?

The typical way one would do so with a Firefox webdriver does not work. 使用Firefox网络驱动程序执行此操作的典型方式不起作用。

Since the PhantomJS instance already runs, it wouldn't make sense to pass commandline options to the RemoteDriver constructor. 由于PhantomJS实例已经运行,因此将命令行选项传递给RemoteDriver构造函数没有任何意义。 There is a way though. 不过有办法。

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). PhantomJS本身支持通过phantom.setProxy(ip, port, type, un, pw)配置代理的编程方式(未记录,但自PhantomJS 2起可用)。 This has to be executed in the phantom context, so driver.execute_script() won't work here. 这必须在幻像上下文中执行,因此driver.execute_script()在这里无法使用。

GhostDriver accepts such script that are to be executed in the phantom context through a special command which you can invoke like this ( source ): GhostDriver接受这样的脚本,这些脚本将通过特殊命令在幻像上下文中执行,您可以像这样调用此命令( source ):

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM