You need to enable JavaScript to run this app. 如何使用Selenium+Java自动点击谷歌Chrome 91弹窗的「允许」按钮
优惠活动
大模型
产品
解决方案
定价
更多

如何使用Selenium+Java自动点击谷歌Chrome 91弹窗的「允许」按钮

Chrome 91版本Selenium屏蔽通知弹窗问题说明

根据查阅的资料,谷歌Chrome版本以50为分界存在不同的处理方案,当前使用的是Chrome 91版本。

现有公开解决方案实现逻辑如下:

//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自动点击弹窗的「允许」按钮才能继续执行后续流程。

内容的提问来源于stack exchange,提问作者mastercool

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.13 12:18:01