使用 Chrome Selenium WebDriver 时,它会在服务器启动时输出诊断输出:
在端口 9515 上启动 ChromeDriver (v2.0)
我不想看到这些消息,我该如何压制它们?
我这样做
ChromeOptions options = new ChromeOptions();
options.AddArgument("--silent");
IWebDriver Driver = new ChromeDriver(options);
但诊断输出不会被抑制。
使用 Chrome Selenium WebDriver 时,它会在服务器启动时输出诊断输出:
在端口 9515 上启动 ChromeDriver (v2.0)
我不想看到这些消息,我该如何压制它们?
我这样做
ChromeOptions options = new ChromeOptions();
options.AddArgument("--silent");
IWebDriver Driver = new ChromeDriver(options);
但诊断输出不会被抑制。
我只是这样做
ChromeOptions options = new ChromeOptions();
options.AddArgument("--log-level=3");
IWebDriver driver = new ChromeDriver(options);
好问题,但是,我不知道你从哪里得到那个.AddArgument("--silent");东西,因为那是 Chrome 的命令行开关,而不是 ChromeDriver。--silent此外,无论如何都没有调用 Chrome 开关。
在OpenQA.Selenium.Chrome命名空间下,有一个类被调用ChromeDriverService,它的属性SuppressInitialDiagnosticInformation默认为 false。基本上,您可能想要做的是创建
ChromeDriverService并将其传递给 ChromeDriver 的构造函数。请参阅此处的文档。
这是抑制 ChromeDriver 诊断输出的 C# 代码。
ChromeOptions options = new ChromeOptions();
ChromeDriverService service = ChromeDriverService.CreateDefaultService();
service.SuppressInitialDiagnosticInformation = true;
IWebDriver driver = new ChromeDriver(service, options);
编辑:
ChromeDriver(不是 Chrome)有一个命令行参数--silent,它应该可以工作。SuppressInitialDiagnosticInformation在 .NET 绑定中正是这样做的。但是,它似乎只压制了一些消息。
这是一个封闭的 chromedriver 票证: 问题 116:如何从 Chrome 驱动程序禁用诊断消息和日志文件?
对我来说,以前的任何一个答案都没有帮助,我的解决方案是:
ChromeDriverService service = ChromeDriverService.CreateDefaultService(driverLocation);
service.SuppressInitialDiagnosticInformation = true;
service.HideCommandPromptWindow = true;
var driver = new ChromeDriver(service, options);
对我来说唯一有用的东西
selenium-chrome-driver-2.48.2.jar chromedriver 2.20 selenium-java-2.48.2.jar
曾是
ChromeOptions options = new ChromeOptions(); System.setProperty("webdriver.chrome.args", "--disable-logging"); System.setProperty("webdriver.chrome.silentOutput", "true"); driver = new ChromeDriver(options);
试试这段代码,它会用“headless”参数隐藏浏览器,但 Chrome 版本应该 > 58
(甚至你可以隐藏命令提示符窗口)
IWebDriver driver;
ChromeOptions options = new ChromeOptions();
options.AddArguments("--disable-extensions");
options.AddArgument("test-type");
options.AddArgument("--ignore-certificate-errors");
options.AddArgument("no-sandbox");
options.AddArgument("--headless");//hide browser
ChromeDriverService service = ChromeDriverService.CreateDefaultService(@"chromedriverExepath\");
service.SuppressInitialDiagnosticInformation = true;
//service.HideCommandPromptWindow = true;//even we can hide command prompt window (with un comment this line)
options.BinaryLocation = @"C:\Program Files (x86)\Google\Chrome\Application\chrome.exe";
driver = new ChromeDriver(service, options);
driver.Manage().Window.Maximize();
driver.Navigate().GoToUrl("https://www.example.com");
要在控制台中以完全静默模式运行带有 Selenium 的 Chrome 浏览器,您应该使用以下代码段:
options = Options()
options.headless = True
options.add_experimental_option("excludeSwitches", ["enable-logging"])
该技巧将抑制来自 Selenium 驱动程序或浏览器本身的任何控制台消息,包括一DevTools listening on ws://127.0.0.1开始的第一条消息。
对于任何在这里发现自己想要 Java 解决方案的人,这里有一个线程:
这段代码对我来说很好:
public static IWebDriver Driver { set; get; }
-----
Driver = CreateBrowserDriver();
////////////// Create Driver
private static IWebDriver CreateBrowserDriver()
{
try
{
var options = new OpenQA.Selenium.Chrome.ChromeOptions();
options.AddArguments("--disable-extensions");
options.AddArgument("--headless"); // HIDE Chrome Browser
var service = OpenQA.Selenium.Chrome.ChromeDriverService.CreateDefaultService();
service.HideCommandPromptWindow = true; // HIDE Chrome Driver
service.SuppressInitialDiagnosticInformation = true;
return new OpenQA.Selenium.Chrome.ChromeDriver(service, options);
}
catch
{
throw new Exception("Please install Google Chrome.");
}
}
////////////// Exit Driver
public static void ExitDriver()
{
if (Driver != null)
{
Driver.Quit();
}
Driver = null;
try
{
// Chrome
System.Diagnostics.Process.GetProcessesByName("chromedriver").ToList().ForEach(px => px.Kill());
}
catch { }
}