Home > Enterprise >  How do I choose to "always allow" in the popup notification in Google Chrome with Selenium
How do I choose to "always allow" in the popup notification in Google Chrome with Selenium

Time:10-01

Popup notification

I am creating a program to automate whatsapp messages to some clients, but a notification appears on the screen, I would like to press always allow to advance in the process. Using Selenium with java.

I already tried using code to deactivate the pop-up notifications but it doesn't work, so I would like to accept the permission so that the notification does not appear, I want to change the permissions so that the popup notification does not appear.

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;

   }
}

I wait your answer thank you!

CodePudding user response:

// 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.

CodePudding user response:

I had a similar issue some time ago. As far as I remember it's not really possible to interact with those popups using selenium since it's not a part of website but rather something rendered by the browser itself.

You can try disabling the popups via ChromeOptions like this

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

depending on your chromedriver version this might not work. If so, try the following:

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

references:

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

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

  • Related