如何使用 xvfb 将 Chrome 驱动程序服务与无头所需的功能合并?

1 java selenium-grid selenium-webdriver mutablecapabilities

我想ChromeDriverService与xvfb 中运行的浏览器chromeOptions合并。DesiredCapabilities

下面是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);
Run Code Online (Sandbox Code Playgroud)

下面是 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);
    }
Run Code Online (Sandbox Code Playgroud)

我知道我可以用于addArguments("--headless")chrome,但它不能很好地与 my webApp 配合使用。而且我也使用了DesiredCapabilities.merge错误。

如何使用 or 合并代码ChromeDriverService/ChromeOptions配置DesiredCapabilites

Deb*_*anB 5

正如你提到的你想要合并ChromeDriverService或者ChromeOptions两者DesiredCapabilities都可以实现。但从当前Selenium Java Client版本开始,以下构造函数已被弃用

ChromeDriver(Capabilities capabilities)
//and
ChromeDriver(ChromeDriverService service, Capabilities capabilities)
Run Code Online (Sandbox Code Playgroud)

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

  • 选项 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);
    
    Run Code Online (Sandbox Code Playgroud)
  • 选项 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);
    
    Run Code Online (Sandbox Code Playgroud)