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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13886430/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 14:19:57  来源:igfitidea点击:

Passing options to chrome driver selenium

javaseleniumwebdriverselenium-chromedriver

提问by Lazadon

I am trying to disable the output to the console for chrome. If I pass the --start-maximized option it works fine. I may have the wrong command?

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

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

I also tried

我也试过

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

Output

输出

Started ChromeDriver port=26703 version=23.0.1240.0 log=/Brett/workspace/TestNG/chromedriver.log [1214/161331:ERROR:ipc_sync_channel.cc(378)] Canceling pending sends [1214/161331:ERROR:ipc_sync_channel.cc(378)] Canceling pending sends [1214/161331:ERROR:ipc_sync_channel.cc(378)] Canceling pending sendsBlockquote

启动 ChromeDriver port=26703 version=23.0.1240.0 log=/Brett/workspace/TestNG/chromedriver.log [1214/161331:ERROR:ipc_sync_channel.cc(378)] 取消挂起发送 [1214/161331:ERROR:ccc_sync_channel. 378)] 取消挂起的发送 [1214/161331:ERROR:ipc_sync_channel.cc(378)] 取消挂起的发送块引用

采纳答案by Rob W

Hinted by this Chromedriver ticket(about the silentoption), I looked in the source of ChromeDriverService.java, and found a reference to "webdriver.chrome.logfile".

受此Chromedriver 票证(关于silent选项)的暗示,我查看了 的来源ChromeDriverService.java,并找到了对"webdriver.chrome.logfile".

After adding -Dwebdriver.chrome.logfile="/dev/null"to my javacommand, the logs became readable again: The usless ChromeDriver logs were gone, while theSystem.out.printlncalls and exceptions are still shown in the console.

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

I start javawith the following parameters (Linux / Mac):

我从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

If you're on Windows:

如果您使用的是 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


Explanation for the composition of my classpath (-cp): My tests are located in a directory at "$DIR/output". The Selenium jar file is placed in "$DIR/bin/selenium-server-standalone-2.33.0.jar". "AllTests" is the name of my class containing public static void main(String[] args)- this launches my tests.

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

The other parameters are self-explanatory, adjust it to your needs. For convenience (used in a shell/batch script), I've declared the common directory in a variable DIR.

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

回答by Dan

When I was setting chrome up with

当我设置 chrome 时

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

none of the above answers worked for me, Since I see some of the answers are a few years old, I will post what worked for me.

    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-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);

回答by Ashwin Prabhu

Try "--disable-logging" instead.

试试“ --disable-logging”。

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

回答by Fernando Martín Besteiro

As of Selenium 3 at least, you can use ChromeDriverService and its inner class Builder to be able to launch the driver in silent mode.

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

A oneliner for that:

一个oneliner:

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

The constructor is straight-forward, you create a new service builder setting silent to true (that's the critical part) and you finally build it into a ChromeDriverService which is required by ChromeDriver's constructor.

构造函数很简单,您创建了一个新的服务构建器,设置为 true(这是关键部分),最后将其构建到 ChromeDriver 的构造函数所需的 ChromeDriverService 中。