首页 > 解决方案 > 如何在没有版本信息的情况下启动 Headless Chrome?

问题描述

我有一些运行无头 chrome 的代码,我从教程中复制了它,它运行良好。唯一的问题是它输出一些我不需要的信息:

这会破坏我程序的输出,我该如何关闭它?

这是我的代码:...

package chromeheadlesstest;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import org.apache.commons.io.FileUtils;
import org.openqa.selenium.By;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;

public class ChromeHeadlessTest {
    public static void main(String[] args) throws IOException{
        String chromeDriverPath = "F:\\Jrun\\lib\\chromedriver.exe" ;
        System.setProperty("webdriver.chrome.driver", chromeDriverPath);
        ChromeOptions options = new ChromeOptions();
        options.addArguments("--headless", "--disable-gpu", "--window-size=1920,1200","--ignore-certificate-errors", "--silent");
        options.addArguments("user-agent=Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.77 Safari/537.36");
        WebDriver driver = new ChromeDriver(options);
        driver.get("https://bbc.co.uk");
        String page = driver.getPageSource();
        FileWriter Details = new FileWriter("I:\\cak\\page1.html", false);
        Details.write(page);
        Details.close();
        // Take a screenshot of the current page
        File screenshot = ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE);
        FileUtils.copyFile(screenshot, new File("screenshot.png"));      
    }
}

...

标签: google-chromeheadless

解决方案


推荐阅读