将选项传递给chrome驱动程序selenium

我试图禁用输出到控制台的chrome。 如果我通过–start-maximized选项,它可以正常工作。 我可能有错误的命令?

DesiredCapabilities capabilities = DesiredCapabilities.chrome(); capabilities.setCapability("chrome.switches", Arrays.asList("--silent")); chrome = new ChromeDriver(_chromeservice,capabilities); 

我也试过了

  ChromeOptions options = new ChromeOptions(); options.addArguments("silent"); chrome = new ChromeDriver(options); 

产量

已启动ChromeDriver port = 26703 version = 23.0.1240.0 log = / Brett / workspace / TestNG / chromedriver.log [1214/161331:ERROR:ipc_sync_channel.cc(378)]取消待处理的发送[1214/161331:错误:ipc_sync_channel.cc( 378)]取消待处理的发送[1214/161331:错误:ipc_sync_channel.cc(378)]取消待处理的sendBlockquote

通过这张Chromedriver票证 (关于silent选项)暗示,我查看了ChromeDriverService.java的来源,并找到了对"webdriver.chrome.logfile"的引用。

-Dwebdriver.chrome.logfile="/dev/null"到我的java命令后,日志再次可读:无用的ChromeDriver日志消失了,而System.out.println调用和exception仍然显示在控制台中。

我使用以下参数启动java (Linux / Mac):

 DIR=path/to/dir/containing/selenium/and/stuff cd "$DIR" && java -cp "$DIR\ :$DIR/output\ :$DIR/bin/selenium-server-standalone-2.33.0.jar" \ -Dwebdriver.chrome.driver="$DIR/bin/chromedriver" \ -Dwebdriver.chrome.args="--disable-logging" \ -Dwebdriver.chrome.logfile="/dev/null" \ AllTests 

如果你在Windows上:

 set DIR=path\to\dir\containing\selenium\and\stuff cd "%DIR%" && java -cp "%DIR%;%DIR%\output;%DIR%\bin\selenium-server-standalone-2.33.0.jar" ^ -Dwebdriver.chrome.driver="%DIR%\bin\chromedriver.exe" ^ -Dwebdriver.chrome.args="--disable-logging" ^ -Dwebdriver.chrome.logfile=NUL ^ AllTests 

我的类路径( -cp )的组成说明:我的测试位于“$ DIR / output”目录中。 Selenium jar文件放在“$ DIR / bin / selenium-server-standalone-2.33.0.jar”中。 “AllTests”是包含public static void main(String[] args)类的名称 – 这将启动我的测试。

其他参数不言自明,可根据您的需要进行调整。 为方便起见(在shell / batch脚本中使用),我在变量DIR声明了公共目录。

请尝试“ --disable-logging ”。

 DesiredCapabilities capabilities = DesiredCapabilities.chrome(); capabilities.setCapability("chrome.switches", Arrays.asList("--disable-logging")); chrome = new ChromeDriver(_chromeservice,capabilities); 

当我设置chrome时

  selenium-chrome-driver-2.48.2.jar chromedriver 2.20 selenium-java-2.48.2.jar 

上述答案都没有对我有用,因为我看到一些答案已经有几年了,我会发布对我有用的东西。

  ChromeOptions chromeOptions = setupChromeOptions(); System.setProperty("webdriver.chrome.logfile", "\\path\\chromedriver.log"); System.setProperty("webdriver.chrome.driver", "\\path\\chromedriver.exe"); System.setProperty("webdriver.chrome.args", "--disable-logging"); System.setProperty("webdriver.chrome.silentOutput", "true"); driver = new ChromeDriver(chromeOptions); 

至少从Selenium 3开始,您可以使用ChromeDriverService及其内部类Builder来以静默模式启动驱动程序。

一个oneliner:

 new ChromeDriver(new ChromeDriverService.Builder().withSilent(true).build()); 

构造函数是直截了当的,您创建一个新的服务构建器设置静默为true(这是关键部分),然后您最终将其构建为ChromeDriver的构造函数所需的ChromeDriverService。