【问题标题】:How do I choose to "always allow" in the popup notification in Google Chrome with Selenium/java?如何使用 Selenium/java 在 Google Chrome 的弹出通知中选择“始终允许”?
【发布时间】:2021-11-20 21:28:37
【问题描述】:

Popup notification

我正在创建一个程序来自动向某些客户发送 whatsapp 消息,但屏幕上会出现一条通知,我想在此过程中按始终允许前进。在 java 中使用 Selenium。

我已经尝试使用代码来停用弹出通知但它不起作用,所以我想接受权限以便不出现通知,我想更改权限以便弹出通知出现不出现。

import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.interactions.Actions;

/**
*
* @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>();
prefs.put("profile.default_content_setting_values.notifications", 2);
prefs.put("credentials_enable_service", false);
prefs.put("profile.password_manager_enabled", false);
prefs.put("profile.managed_default_content_settings.notifications", 1);

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



       
       WebDriver wd = new ChromeDriver(options);
      

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

       return 0;

   }
}

我等你的回答谢谢!

【问题讨论】:

标签: java selenium google-chrome selenium-webdriver automation


【解决方案1】:
// Try using chrome options as below.  
  Map<String, Object> pref = new HashMap<String, Object>();
    prefs.put("profile.default_content_setting_values.notifications", 2);
    ChromeOptions options = new ChromeOptions();
    options.setExperimentalOption("prefs", prefs);
    WebDriver driver = new ChromeDriver(options);

// if it does not work , you need to check outside selenium.

【讨论】:

    【解决方案2】:

    前段时间我也遇到过类似的问题。据我所知,使用 selenium 与这些弹出窗口进行交互是不可能的,因为它不是网站的一部分,而是浏览器本身呈现的东西。

    您可以尝试像这样通过 ChromeOptions 禁用弹出窗口

    options.addArguments("--disable-popup-blocking");

    根据您的 chromedriver 版本,这可能不起作用。如果是这样,请尝试以下操作:

    ChromeOptions options = new ChromeOptions(); options.setExperimentalOption("excludeSwitches",Arrays.asList("disable-popup-blocking")); caps.setCapability(ChromeOptions.CAPABILITY, options);

    参考:

    https://www.browserstack.com/docs/automate/selenium/enable-pop-ups

    https://newbedev.com/how-to-click-allow-on-show-notifications-popup-using-selenium-webdriver

    【讨论】:

      猜你喜欢
      • 2021-09-18
      • 2021-08-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-02-23
      • 1970-01-01
      • 1970-01-01
      • 2013-04-23
      相关资源
      最近更新 更多