【问题标题】:How to Merge Chrome driver service with desired capabilities for headless using xvfb?如何使用 xvfb 将 Chrome 驱动程序服务与所需的无头功能合并?
【发布时间】:2018-05-20 04:36:56
【问题描述】:

我想将ChromeDriverServicechromeOptionsDesiredCapabilities 合并以在xvfb 中运行浏览器。

下面是代码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 和错误。

如何将代码/配置 ChromeDriverServiceChromeOptionsDesiredCapabilites 合并?

【问题讨论】:

  • Hey@Mysound Magic 欢迎来到堆栈溢出,请记住问题发布的格式应正确且易于理解。

标签: java selenium-webdriver selenium-grid mutablecapabilities


【解决方案1】:

正如您所提到的,您希望将 ChromeDriverServiceChromeOptionsDesiredCapabilities 两者都可以实现。但在当前 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,然后使用MutableCapabilities 中的merge()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);
    

【讨论】:

  • 非常感谢您的回答。我试过你的代码,它适用于在 xvfb 中运行浏览器,但不适用于 selenium 网格。我认为由于 driver = new RemoteWebDriver(service.getUrl(), options) url 没有转到节点。因为在我的代码中我使用 NodeChromeIncognito。这是节点的url。 String NodeChromeIncognito = "localhost:5558/wd/hub"
  • @DebanjanB option.merge 显示错误 chromeOptions 类型未定义方法合并(DesiredCapabilities)
猜你喜欢
  • 2019-02-23
  • 2016-02-22
  • 1970-01-01
  • 1970-01-01
  • 2021-04-11
  • 1970-01-01
  • 2017-06-27
  • 2021-09-08
  • 1970-01-01
相关资源
最近更新 更多