【问题标题】:How to disable notifications using ChromeOptions through Selenium WebDriver如何通过 Selenium WebDriver 使用 ChromeOptions 禁用通知
【发布时间】:2019-03-12 12:07:43
【问题描述】:

在使用 Selenium Webdriver 进行测试时,我试图阻止 Chrome 通知。我曾尝试使用网站上其他地方记录的 Java 命令,但每次尝试添加时,我的附加代码都会被标记为错误。

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;

/**
 * @author Me
 */
public class MyTest {
    static WebDriver webDriver;
    /**
     * @param args
     * @throws InterruptedException
     */
    public static void main(final String[] args) throws InterruptedException {
        // Telling the system where to find the chrome driver
        System.setProperty(
                "webdriver.chrome.driver",
                "C:/Users/Me/Documents/WebDriver/chromedriver_win32/chromedriver.exe");

        // Open the Chrome browser
        webDriver = new ChromeDriver();
        webDriver.manage().window().maximize();

我正在尝试将以下命令添加到我的代码中,但它们不起作用:

ChromeOptions ops = new ChromeOptions();
  ops.addArguments("--disable-notifications");

谁能告诉我需要在哪里将它们添加到我的代码 sn-p 中?我尝试将它们插入到 System.setProperty 上方,但它不起作用。

【问题讨论】:

    标签: java google-chrome selenium-webdriver webdriver selenium-chromedriver


    【解决方案1】:

    要添加 参数 --disable-notifications,您需要初始化 ChromeOptions 的实例,并在初始化 ChromeDriver 时传递该实例 / Chrome 浏览器 实例如下:

    import org.openqa.selenium.WebDriver;
    import org.openqa.selenium.chrome.ChromeDriver;
    import org.openqa.selenium.chrome.ChromeOptions;
    
    /**
     * @author Me
     */
    public class MyTest {
        static WebDriver webDriver;
        /**
         * @param args
         * @throws InterruptedException
         */
        public static void main(final String[] args) throws InterruptedException {
            // Telling the system where to find the chrome driver
            System.setProperty("webdriver.chrome.driver", "C:/Users/Me/Documents/WebDriver/chromedriver_win32/chromedriver.exe");
            ChromeOptions ops = new ChromeOptions();
            ops.addArguments("--disable-notifications");
            ops.addArguments("start-maximized");
            // Open the Chrome browser
            webDriver = new ChromeDriver(ops);
    

    注意:不要使用webDriver.manage().window().maximize(),而是使用ChromeOptions 对象。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-08-09
      • 2018-08-08
      • 2016-03-24
      • 1970-01-01
      • 2015-12-16
      相关资源
      最近更新 更多