【问题标题】:How to click "Allow" automatically on Google Chrome pop-up using Selenium + Java如何使用 Selenium + Java 在 Google Chrome 弹出窗口上自动单击“允许”
【发布时间】:2021-09-18 00:42:47
【问题描述】:

根据我所阅读的内容,有一种方法可以用于 Google Chrome 版本 50。我使用的是 Google Chrome 91。

这里有一个答案: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);

但是,这对我不起作用。这是弹出窗口的样子

这就是我使用它的方式:

// 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); 也无效 当我运行我的程序时,弹出窗口仍然存在,我需要在 Selenium 中单击弹出窗口上的“允许”,以便我可以继续执行程序的其余部分。

【问题讨论】:

  • 你也试过prefs.put("profile.default_content_setting_values.notifications", 1);吗?
  • 是的,也试过了
  • 1 是允许,2 是阻止那里的通知,但这似乎不再适用于 Chrome 版本 >90
  • 您是说这不适用于 > 90 的版本?
  • 我在 python 中使用了 91 并且效果很好

标签: java selenium google-chrome chrome-options


【解决方案1】:

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

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

【讨论】:

猜你喜欢
  • 1970-01-01
  • 2015-02-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-11-20
  • 2021-12-05
  • 2016-03-18
相关资源
最近更新 更多