通过驱动程序ChromeOptions和DesiredCapabilities?

时间:2014-05-23 16:39:09

标签: selenium selenium-webdriver webdriver

新的Google Chrome更新会在浏览器中显示此消息“您正在使用不受支持的命令行标记:--ignore-certificate-errors。稳定性和安全性将受到影响。”

从我读到的关于selenium bug报告的内容来看,临时解决方案是使用

启动webdriver
options.AddArgument("test-type")

我在创建驱动程序时已经传递了DesiredCapabilities。如何将ChromeOptions和DesiredCapabilities传递给驱动程序?

DesiredCapabilities capabilities = DesiredCapabilities.chrome();
capabilities.setCapability("chrome.switches", Arrays.asList("--incognito"));

ChromeOptions options = new ChromeOptions();
options.addArguments("test-type");

WebDriver driver = new ChromeDriver(capabilities);

5 个答案:

答案 0 :(得分:19)

我正在解决同样的问题,我终于破解了它。基本上,您可以将ChromeOptions添加到DesiredCapabilities,然后使用DesiredCapabilities创建驱动程序。一旦我尝试了它,它就摆脱了横幅。无论如何,这是代码:

DesiredCapabilities capabilities = DesiredCapabilities.chrome();
ChromeOptions options = new ChromeOptions();
options.addArguments("test-type");
capabilities.setCapability("chrome.binary", "<Path to binary>");
capabilities.setCapability(ChromeOptions.CAPABILITY, options);
webDriver = new ChromeDriver(capabilities);

答案 1 :(得分:5)

添加我的答案,以帮助仍在使用Selenium最新版本寻找ChromeOptions和功能的用户。

将功能传递给ChromeDriver构造函数已从v3.6.0弃用。 Source code Reference

ChromeDriver(Capabilities capabilities)
Deprecated. 
Use ChromeDriver(ChromeOptions) instead.

相反,您应该开始使用以下内容:

ChromeOptions options = new ChromeOptions();
options.setAcceptInsecureCerts(true);
options.setUnhandledPromptBehaviour(UnexpectedAlertBehaviour.ACCEPT);
WebDriver driver = new ChromeDriver(options)

您仍然可以定义自己的功能,但请确保将这些功能与ChromeOptions合并,如下所示:

DesiredCapabilities capabilities = new DesiredCapabilities();
capabilities.setCapability("something", true);

ChromeOptions options = new ChromeOptions();
options.addArguments("disable-infobars");
options.merge(capabilities);

您可以查看Selenium ChromeOptions上的文章,了解有关ChromeOptions和功能的详细用法

答案 2 :(得分:1)

仅供参考:此技术似乎不适用于Chrome 41.0.2272.101m。

ChromeOptions chromeOptions = new ChromeOptions();
chromeOptions.AddExcludedArgument("ignore-certificate-errors");
capabilities = DesiredCapabilities.Chrome();
capabilities.SetCapability(ChromeOptions.Capability, chromeOptions);

new RemoteWebDriver(hubUri, capabilities, TimeSpan.FromSeconds(180))

生成

09:39:24.110 WARN - Exception: unknown error: cannot parse capability: chromeOptions
from unknown error: unrecognized chrome option: Arguments
(Driver info: chromedriver=2.10.267521,platform=Windows NT 6.3 x86_64) 
(WARNING: The server did not provide any stacktrace information)
Command duration or timeout: 1.52 seconds
Build info: version: '2.43.0', revision: '597b76b', time: '2014-09-09 20:52:14'
System info: host: 'T10-E8-GREGPE', ip: '10.123.160.70', os.name: 'Windows 8.1', os.arch: 'x86', os.version: '6.3', java.version: '1.8.0_40'
Driver info: org.openqa.selenium.chrome.ChromeDriver

这似乎有效:

ChromeOptions chromeOptions = new ChromeOptions();
chromeOptions.AddArgument("test-type");
capabilities = (DesiredCapabilities)chromeOptions.ToCapabilities();

new RemoteWebDriver(hubUri, capabilities, TimeSpan.FromSeconds(180));

答案 3 :(得分:0)

可以试试这个:

chromeOptions = webdriver.ChromeOptions()
prefs = {"profile.managed_default_content_settings.images":2}
chromeOptions.add_experimental_option("prefs",prefs)
driver = webdriver.Chrome(chrome_options=chromeOptions)

相关信息可在此处找到: http://nullege.com/codes/search/selenium.webdriver.ChromeOptions.add_experimental_option

答案 4 :(得分:0)

public void setUp() throws Exception
{
    System.out.println("Launching Google browser");
    System.setProperty("webdriver.chrome.driver", "C:\\chromedriver.exe");   

    ChromeOptions options = new ChromeOptions();
    options.addArguments("disable-infobars");
    Map<String, Object> prefs = new HashMap<String, Object>();
    prefs.put("profile.default_content_setting_values.automatic_downloads", 1);
    options.setExperimentalOption("prefs", prefs);
    prefs.put("download.default_directory", "C:\\Downloads");
    driver = new ChromeDriver(options);
    driver.manage().timeouts().implicitlyWait(100, TimeUnit.SECONDS);
    driver.manage().window().maximize(); 
}