You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用Selenium+Java操作谷歌Chrome时如何点击弹窗通知的始终允许选项

问题原因

你的代码存在两处冲突配置,导致通知权限设置不生效:

  • 权限配置冲突:prefs中同时设置了profile.default_content_setting_values.notifications = 2(禁用通知)和profile.managed_default_content_settings.notifications = 1(允许通知),优先级规则混乱导致配置未生效
  • 启动参数强制禁用:你添加了--disable-notifications启动参数,该参数会全局禁用浏览器所有通知权限请求,直接覆盖prefs中的权限配置,无法完成允许操作

修改方案

删除冲突的禁用配置,仅保留允许通知的配置即可,修改后的完整代码如下:

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.interactions.Actions;
import java.util.HashMap;
import java.util.Map;

/**
*
* @author DiseñoVerde
*/
public class Abrir_con_Chrome {
   
 
 public int enviarnumero(int numero) {

       int num = numero;
       
       System.setProperty("webdriver.chrome.driver", "C:\\Users\\DiseñoVerde\\OneDrive\\WhatsappDirect\\driver\\chromedriver.exe");

       ChromeOptions options = new ChromeOptions();
       
       Map<String, Object> prefs = new HashMap<String, Object>();
       // 1代表默认允许所有站点通知权限
       prefs.put("profile.managed_default_content_settings.notifications", 1);
       prefs.put("credentials_enable_service", false);
       prefs.put("profile.password_manager_enabled", false);

       options.setExperimentalOption("prefs", prefs);
       options.addArguments("start-maximized");
       options.addArguments("disable-infobars");
       options.addArguments("--disable-extensions");

       WebDriver wd = new ChromeDriver(options);

       Actions builder = new Actions(wd);
       
       wd.get("https://wa.me/505" + num);

       return 0;

   }
}

修改后启动Chrome会默认允许所有站点的通知权限请求,不会再弹出通知授权弹窗。


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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.02 11:27:01