How to Merge Chrome driver service with desired capabilities for headless using xvfb?

只谈情不闲聊 提交于 2019-11-29 12:47:18

As you mentioned you want to merge ChromeDriverService with ChromeOptions or with DesiredCapabilities both can be achieved. But as of current Selenium Java Client releases the following Constructors are Deprecated :

ChromeDriver(Capabilities capabilities)
//and
ChromeDriver(ChromeDriverService service, Capabilities capabilities)

Hence we have to use either of the following options :

  • Option A : Use only ChromeOptions :

    private static ChromeDriverService service;
    private WebDriver driver;
    //code block
    service = new ChromeDriverService.Builder()
     .usingDriverExecutable(new File("path/to/my/chromedriver.exe"))
     .usingAnyFreePort()
     .build();
    chromeDriverService.start();
    
    ChromeOptions option = new ChromeOptions();
    option.addArguments("--incognito");
    driver = new RemoteWebDriver(service.getUrl(), options);
    
  • Option B : Use DesiredCapabilities and then use merge() from MutableCapabilities to merge within ChromeOptions :

    private static ChromeDriverService service;
    private WebDriver driver;
    //code block
    service = new ChromeDriverService.Builder()
     .usingDriverExecutable(new File("path/to/my/chromedriver.exe"))
     .usingAnyFreePort()
     .build();
    chromeDriverService.start();        
    DesiredCapabilities capabilities = new DesiredCapabilities();
    capabilities.setCapability("...", true);
    ChromeOptions option = new ChromeOptions();
    option.addArguments("--incognito");
    option.merge(capabilities);
    driver = new RemoteWebDriver(service.getUrl(), options);
    
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!