1

我想合并或合并ChromeDriverService以在 xvfb 中运行浏览器。chromeOptionsDesiredCapabilities

ChromeDriverService下面是我之前在没有硒网格的情况下使用的部分代码。

String NodeChromeIncognito = "http://localhost:5558/wd/hub"

         ChromeDriverService chromeDriverService = new ChromeDriverService.Builder()
         .usingDriverExecutable(new File("driver_linux/chromedriver"))
         .usingAnyFreePort()
         .withEnvironment(ImmutableMap.of("DISPLAY", ":1")).build();
     chromeDriverService.start();
    // commented because using RemoteWebDriver
    // driver = new ChromeDriver(chromeDriverService);

下面是我将合并的 RemoteWebDriver 的部分代码ChromeDriverService

DesiredCapabilities cap = null;
String NodeChromeIncognito = "http://localhost:5558/wd/hub";
String NodeChrome = "http://localhost:5557/wd/hub";
String NodeFirefox = "http://localhost:5556/wd/hub";

    if (browserName.equals("chrome")) {
        cap = DesiredCapabilities.chrome();
        cap.setBrowserName("chrome");
        driver = new RemoteWebDriver(new URL(NodeChrome), cap);
    } else if (browserName.equals("firefox")) {
        System.setProperty("webdriver.gecko.driver", "driver_linux/geckodriver");
        cap = DesiredCapabilities.firefox();
        cap.setCapability("marionette", true);
        driver = new RemoteWebDriver(new URL(NodeFirefox), cap);
    }else if (browserName.equals("chromeIncognito")) {
        ChromeOptions option = new ChromeOptions();
        option.addArguments("--incognito");

        cap = DesiredCapabilities.chrome();
        cap.setCapability(ChromeOptions.CAPABILITY, option);
        cap.setPlatform(Platform.WIN10);
        cap.setBrowserName("chrome incognito");
        driver = new RemoteWebDriver(new URL(NodeChromeIncognito), cap);
    }

我知道我可以addArguments("--headless")用于 chrome,但它不适用于我的 webApp。而且我也使用过DesiredCapabilities.merge错误。

如何将代码/配置ChromeDriverServiceChromeOptionsor合并DesiredCapabilites

4

1 回答 1

4

正如您所提到的,您希望与两者合并ChromeDriverServiceChromeOptions两者DesiredCapabilities都可以实现。但在当前Selenium Java Client版本中,以下构造函数已被弃用

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

因此,我们必须使用以下任一选项:

  • 选项 A:仅使用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);
    
  • 选项 B:使用DesiredCapabilities,然后使用merge()fromMutableCapabilities合并到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);
    
于 2017-12-06T11:10:10.493 回答