如何解决如何使用 Selenium + Java 在 Google Chrome 弹出窗口中自动单击“允许”
根据我所读到的内容,有一种方法可以针对 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>();
// 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.automatic_downloads",1);
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。