gpt4 book ai didi

java - 如何使用 java 在 chrome 驱动程序中运行 headless (headless)模式

转载 作者:行者123 更新时间:2023-11-30 02:39:37 30 4
gpt4 key购买 nike

这是我的 chrome 驱动程序代码:-

String driverPath = driverFile.getAbsolutePath();
System.setProperty("webdriver.chrome.driver", driverPath);

Callable<ChromeDriver> startChromedriver = new Callable<ChromeDriver>() {
public ChromeDriver call() {
ChromeOptions Chromeoptions = new ChromeOptions();
Chromeoptions.addArguments("--startMaximized");


caps.setCapability("newCommandTimeout", 300);
caps.setCapability(ChromeOptions.CAPABILITY, Chromeoptions);

return new ChromeDriver(caps);
}
};

我已在终端中启动以下操作:-

Xvfb -ac :99 -screen 0 1280x1024x16 &

export DISPLAY=:99

然后在 Intellij 中开始我的 Junit 测试

如何在 ubuntu 机器上使用 chrome 驱动程序在 java 中运行 junit 测试?

对于 Firefox,我已经尝试过了,它的工作原理。

apt-get update

sudo apt-get install xvfb

sudo apt-get install -y xorg xvfb dbus-x11 xfonts-100dpi xfonts-75dpi xfonts-cyrillic


sudo Xvfb :2 -ac


export DISPLAY=:2

在 Firefox 中设置

// Setup firefox binary to start in Xvfb
String Xport = System.getProperty(
"lmportal.xvfb.id", ":2");
final File firefoxPath = new File(System.getProperty(
"lmportal.deploy.firefox.path", "/usr/bin/firefox"));
FirefoxBinary firefoxBinary = new FirefoxBinary(firefoxPath);
firefoxBinary.setEnvironmentProperty("DISPLAY", Xport);

// Start Firefox driver
WebDriver driver = new FirefoxDriver(firefoxBinary, null);
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
driver.get("http://google.com/");

System.out.println("page source" + driver.getCurrentUrl());

最佳答案

从 Chrome 59 开始,您甚至不需要 Xvfb。

在此处下载 Chrome 驱动程序:https://chromedriver.storage.googleapis.com/index.html?path=2.38/

或者在 macOS 上:

brew install chromedriver

然后在 pom.xml/graddle 中添加最新版本的 Selenium:

<dependency>  
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>3.8.1</version>
</dependency>

当然,您需要 Chrome 版本 > 59

下面是 Java 部分:

String chromeDriverPath = "/Path/To/Chromedriver" ;  
System.setProperty("webdriver.chrome.driver", chromeDriverPath);
ChromeOptions options = new ChromeOptions();
options.addArguments("--headless", "--disable-gpu", "--window-size=1920,1200","--ignore-certificate-errors");
WebDriver driver = new ChromeDriver(options);

我在这里写了一篇博客文章,其中包含详细说明: https://ksah.in/introduction-to-chrome-headless/

关于java - 如何使用 java 在 chrome 驱动程序中运行 headless (headless)模式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42132952/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com