为 Selenium PhantomJSDriver 指定 PhantomJS 命令行选项 [英] Specify PhantomJS command line optionsto Selenium PhantomJSDriver

查看:27
本文介绍了为 Selenium PhantomJSDriver 指定 PhantomJS 命令行选项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

PhantomJS 驱动程序是否支持命令行参数?我需要使用 PhantomJS 驱动程序运行 Selenium 测试并禁用网络安全.我试过了:

Does the PhantomJS driver support command line arguments? I need to run Selenium tests with the PhantomJS driver and disable web security. I have tried:

PhantomJSOptions options = new PhantomJSOptions();           
options.AddAdditionalCapability("web-security",false);             
driver = new PhantomJSDriver(Environment.CurrentDirectory + @"drivers", options);

但这似乎不起作用.PhantomJSDriver 是否允许传递命令行参数?

but this does not seem to work. Does the PhantomJSDriver allow for passing command line arguments?

推荐答案

您可以使用 PhantomJSDriverService.WebSecurity 属性,而不是将其作为 PhantomJSOptions.

You can specify PhantomJS' --web-security command line options using PhantomJSDriverService.WebSecurity Property, rather than pass it as PhantomJSOptions.

这是在 Selenium 2.32.0 中添加的,引用自 CHANGELOG:

This is added in Selenium 2.32.0, a quote from CHANGELOG:

(代表 GeoffMcElhanon)添加了对传递参数的支持幻影JS.PhantomJSDriverService 现在具有类型安全的属性对于 PhantomJS 支持的所有命令行开关.这些可以直接在命令行传递,也可以序列化到一个 JSON 文件中,用于通过 --config 命令行开关传递到 PhantomJS.

(on behalf of GeoffMcElhanon) Added support to pass arguments to PhantomJS. The PhantomJSDriverService now has type-safe properties for all of the command-line switches supported by PhantomJS. These can be passed directly on the command line, or can be serialized into a JSON file for passing with the --config command line switch to PhantomJS.

以下是未经测试的代码,必要时请参考文档(Selenium zip 文件中的 WebDriver.chm).

Below is the untested code, please refer to the documentation (the WebDriver.chm in your Selenium zip file) when necessary.

var service = PhantomJSDriverService.CreateDefaultService(Environment.CurrentDirectory + @"drivers");
service.WebSecurity = false;
var driver = new PhantomJSDriver(service);

PhantomJSDriverService 有其他可以指定的预定义命令行参数,请查看文档.还有一些方法可以添加您自己的参数.

PhantomJSDriverService has other pre-defined command line arguments one can specify, please check documentation. Also there are methods to add your own arguments.

AddArgument(): 将单个参数添加到要附加到 PhantomJS.exe 命令行的参数列表中.
AddArguments(IEnumerable): 添加要附加到 PhantomJS.exe 命令行的参数.
AddArguments(String[]): 添加要附加到 PhantomJS.exe 命令行的参数.

AddArgument(): Adds a single argument to the list of arguments to be appended to the PhantomJS.exe command line.
AddArguments(IEnumerable): Adds arguments to be appended to the PhantomJS.exe command line.
AddArguments(String[]): Adds arguments to be appended to the PhantomJS.exe command line.

这篇关于为 Selenium PhantomJSDriver 指定 PhantomJS 命令行选项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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