gpt4 book ai didi

java - 如何通过 Selenium WebDriver 使用 ChromeOptions 禁用通知

转载 作者:行者123 更新时间:2023-12-02 10:09:08 25 4
gpt4 key购买 nike

我在使用 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");

谁能告诉我需要将它们添加到我的代码片段中吗?我尝试将它们插入到 System.setProperty 上方,但它不起作用。

最佳答案

要添加参数 --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);

Note: Instead of using webDriver.manage().window().maximize(), use the ChromeOptions object.

关于java - 如何通过 Selenium WebDriver 使用 ChromeOptions 禁用通知,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55121113/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com