Seleniumテストではheadless modeでChromeを使用しようとしています。ここで私は何をすべきかです:Selenium:ChromeDriverのsetHeadless()は無効です
public static void main(String[] args) throws Exception {
RemoteWebDriver driver = new ChromeDriver(new ChromeOptions().setHeadless(true));
try {
driver.navigate().to("https://example.com/");
File file = driver.getScreenshotAs(OutputType.FILE);
Files.copy(file.toPath(), Paths.get("target/screenshot.png"), StandardCopyOption.REPLACE_EXISTING);
// adding a delay to visually check whether there is a browser window
Thread.sleep(1000);
} finally {
driver.close();
}
}
私はChromeOptions
インスタンス上setHeadless(true)
を使用していますが、私はこれを実行すると、クロームウィンドウがポップアップ表示されます。 Chromeでは、そのヘッドレス設定を無視しているようです。
質問はです:なぜChromeは設定を無視し、それを尊重するのですか?
私は、Ubuntu Linux 16.04の下で公式の64ビットビルドであるChrome 63.0.3239.108を持っています。 Seleniumはバージョン3.8.1です。
は、ここに私のpom.xml
次のとおりです。
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.rpuch.test</groupId>
<artifactId>test-selenium-headless-chrome</artifactId>
<version>1.0-SNAPSHOT</version>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>3.8.1</version>
</dependency>
</dependencies>
</project>
プロジェクトはGitHubのに利用可能である:https://github.com/rpuch/test-selenium-headless-chrome