如何在 HeadlessChrome 中解决 "You are using an unsupported command-line flag: --ignore-certificate-errors, Stability and security will suffer"

How to address "You are using an unsupported command-line flag: --ignore-certificate-errors, Stability and security will suffer" in HeadlessChrome

我正在尝试 运行 chrome 在 C# 中使用 selenium 无头,但我不断收到此错误:

You are using an unsupported command-line flag: --ignore-certificate-errors, Stability and security will suffer.

我正在使用

我的代码:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;

namespace MyApp {
public partial class Form1: Form {
    public Form1() {
        InitializeComponent();
    }

    private void StartBtn_Click(object sender, EventArgs e) {
        string appPath = AppDomain.CurrentDomain.BaseDirectory;

        IWebDriver driver;
        ChromeOptions options = new ChromeOptions();
        options.AddArguments("--headless", "--disable-gpu", "--remote-debugging-port=9222", "--window-size=1440,900");
        driver = new ChromeDriver(options);
    }
}
}

我的 WinForm 应用程序只有一个按钮,名称为 "StartBtn"。

消除以下错误:

You are using an unsupported command-line flag: --ignore-certificate-errors, Stability and security will suffer

因为您正在使用 Selenium: 3.6Chrome: 61,而不是使用 chromedriver v2.3 考虑使用最新版本的 chromedriver.exev2.33

此外,除了您现有的参数外,还要添加以下参数:disable-infobars--disable-extensions

所以,代码行如下:

options.AddArguments("headless", "disable-gpu", "remote-debugging-port=9222", "window-size=1440,900", "disable-infobars", "--disable-extensions")

以下是我为解决类似问题所做的工作

ChromeOptions chromeOptions = new ChromeOptions();
chromeOptions.addArguments("--test-type");

以下是打开 url 的完整代码,希望对您有所帮助

    System.setProperty("webdriver.chrome.driver", "C:\Program Files\Java\chromedriver.exe");
    System.out.println(System.getProperty("webdriver.chrome.driver"));
    ChromeOptions chromeOptions = new ChromeOptions();
    chromeOptions.addArguments("no-sandbox");
    chromeOptions.addArguments("--test-type");// this is the one that helped
    chromeOptions.addArguments("disable-extensions");
    chromeOptions.addArguments("--start-maximized");
    WebDriver driver = new ChromeDriver(chromeOptions);
    driver.get("https://www.google.com");
    System.out.println("Google is selected");