如何使用xvfb将具有所需功能的Chrome驱动程序服务合并为无头? [英] How to Merge Chrome driver service with desired capabilities for headless using xvfb?
问题描述
我想将ChromeDriverService
与chromeOptions
或与DesiredCapabilities
合并,以便在xvfb中运行浏览器.
I want to merge ChromeDriverService
with chromeOptions
or with DesiredCapabilities
for running browser in xvfb.
下面是我以前在没有硒网格的情况下使用的代码ChromeDriverService
的一部分.
Below is part of code ChromeDriverService
I previously used without selenium grid.
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);
及以下是我将与ChromeDriverService
合并的RemoteWebDriver的部分代码.
and below is part code of RemoteWebDriver which I will merge with 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
和错误.
I know I could use addArguments("--headless")
for chrome, but it does not work well with the my webApp. And also I used DesiredCapabilities.merge
and error.
如何将代码/配置ChromeDriverService
与ChromeOptions
或DesiredCapabilites
合并?
How to merge code/configuration ChromeDriverService
with ChromeOptions
or DesiredCapabilites
?
推荐答案
如前所述,您想将 ChromeDriverService
与 ChromeOptions
或与合并DesiredCapabilities
都可以实现.但是从当前 Selenium Java Client
版本开始,以下构造函数被不推荐使用:
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 :
-
选项A:仅使用
ChromeOptions
:
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);
这篇关于如何使用xvfb将具有所需功能的Chrome驱动程序服务合并为无头?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!