禁用 Chrome 通知 (Selenium)

Disable Chrome notifications (Selenium)

我只想在由 Selenium Java 应用程序打开的 Chrome 中禁用 Chrome 通知。 (使用 java 代码)

像这样的通知:

问题是关闭浏览器 window 后手动设置的设置会丢失。

此问题已在 "chromedriver-users" google 论坛中得到解答。 这是有效的答案:

Map<String, Object> prefs = new HashMap<String, Object>();
prefs.put("profile.default_content_setting_values.notifications", 2);
ChromeOptions options = new ChromeOptions();
options.setExperimentalOption("prefs", prefs);
WebDriver driver = new ChromeDriver(options);

您可以使用:

chrome_options = Options()
chrome_options.add_argument("--disable-notifications")
browser = webdriver.Chrome(chrome_options=chrome_options)
            ChromeOptions ops = new ChromeOptions();
            ops.addArguments("--disable-notifications");
            System.setProperty("webdriver.chrome.driver", "./lib/chromedriver");
            driver = new ChromeDriver(ops);

有人需要这个用于 Capybara 或 Watir,您可以将 --disable-notifications 作为参数传递,例如 "--start-fullscreen", "--disable-infobars"。以下作品:

Capybara.register_driver :chrome do |app|
  args = ["--disable-notifications"]
  Capybara::Selenium::Driver.new(app, {:browser => :chrome, :args => args})
end
public class MultipleWindowHandle
{
    public static void main(String[] args)
    {
        System.setProperty("webdriver.chrome.driver", "E:\NEWSEL\chromedriver.exe");

        // Create object of HashMap Class as shown below.
        Map<String, Object> prefs = new HashMap<String, Object>();

        // Set the notification setting it will override the default setting.
        prefs.put("profile.default_content_setting_values.notifications", 2);

        // Create object of ChromeOption class.
        ChromeOptions Roptions = new ChromeOptions();

        // Set the experimental option.
        Roptions.setExperimentalOption("prefs", prefs);

        // Open chrome browser.
        ChromeDriver driver = new ChromeDriver(Roptions);
        driver.get("https://my.monsterindia.com/login.html");

        Set<String> id = driver.getWindowHandles();
        Object[] data = id.toArray();
        driver.switchTo().window((String)data[1]); driver.close();
    }
}