有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

如何使用Selenium+Java在Google Chrome弹出窗口上自动单击“允许”

根据我所读到的内容,有一种方法可以为Google Chrome版本做到这一点<;50以及谷歌Chrome版本的一种实现方法>;50.我用的是谷歌Chrome91

这里有一个答案:How to click Allow on Show Notifications popup using Selenium Webdriver 这说明我需要这样做:

//Create a map to store  preferences 
Map<String, Object> prefs = new HashMap<String, Object>();

//add key and value to map as follow to switch off browser notification
//Pass the argument 1 to allow and 2 to block
prefs.put("profile.default_content_setting_values.notifications", 2);

//Create an instance of ChromeOptions 
ChromeOptions options = new ChromeOptions();

// set ExperimentalOption - prefs 
options.setExperimentalOption("prefs", prefs);

//Now Pass ChromeOptions instance to ChromeDriver Constructor to initialize chrome driver which will switch off this browser notification on the chrome browser
WebDriver driver = new ChromeDriver(options);

然而,这对我来说不起作用。这就是弹出窗口的样子pop-up

这就是我使用它的方式:

// Create a map to store preferences (to disable pop-up notifications)
Map<String, Object> prefs = new HashMap<String, Object>();

// add key and value to map as follow to switch off browser notification
// Pass the argument 1 to allow and 2 to block
prefs.put("profile.default_content_setting_values.notifications", 2);

// for local automated testing
this.chromeOptions = new ChromeOptions();
chromeOptions.setExperimentalOption("prefs", prefs);
chromeOptions.addArguments("--disable-notifications");
chromeOptions.addArguments("start-maximized");
String chromeDriverPath = "resources/chromedriver-91.exe";
System.setProperty("webdriver.chrome.driver", chromeDriverPath);
this.driver = new ChromeDriver(chromeOptions);
System.out.println("new chrome driver started.....");
this.driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);

如你所见,我尝试了多种不同的方法"--disable-notifications"不起作用chromeOptions.setExperimentalOption("prefs", prefs); 当我运行我的程序时,弹出窗口仍然存在,我需要单击弹出窗口上的“允许”,以便继续执行程序的其余部分


共 (1) 个答案

  1. # 1 楼答案

    您正在尝试下载文件,这与显示通知的行为不同。试一试

    prefs.put("profile.default_content_setting_values.automatic_downloads", 1);