如何使用 xvfb 将 Chrome 驱动程序服务与所需的无头功能合并?
How to Merge Chrome driver service with desired capabilities for headless using xvfb?
我想在 xvfb 中为 运行 浏览器将 ChromeDriverService
与 chromeOptions
或 DesiredCapabilities
合并。
下面是部分代码ChromeDriverService
我之前使用的是没有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);
以下是 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
和错误。
如何将 code/configuration ChromeDriverService
与 ChromeOptions
或 DesiredCapabilites
合并?
如您所述,您想将 ChromeDriverService
与 ChromeOptions
或 合并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
然后使用 MutableCapabilities
中的 merge()
在 ChromeOptions
[=42= 中合并] :
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 中为 运行 浏览器将 ChromeDriverService
与 chromeOptions
或 DesiredCapabilities
合并。
下面是部分代码ChromeDriverService
我之前使用的是没有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);
以下是 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
和错误。
如何将 code/configuration ChromeDriverService
与 ChromeOptions
或 DesiredCapabilites
合并?
如您所述,您想将 ChromeDriverService
与 ChromeOptions
或 合并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
然后使用MutableCapabilities
中的merge()
在ChromeOptions
[=42= 中合并] :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);