elena
elena

Reputation: 4188

Answer to Chrome's notifications using Puppeteer

Is there a way to reply to Chrome's notifications using puppeteer? I've tried disabling the notifications (I guess then it would just select yes by default?), but it didn't help:

const browser = await puppeteer.launch({headless: false, slowMo: 250, args: ["--disable-notifications"]});

Upvotes: 8

Views: 12496

Answers (5)

Josep Alsina
Josep Alsina

Reputation: 3037

Make sure that the website whose notifications you want overridden does not redirect to a subdomain because then overridePermissions would not work straight away without calling it with the redirected url as well.

Upvotes: 0

Kailash Uniyal
Kailash Uniyal

Reputation: 997

Example: For closing notification pop up (puppeteer)

  const puppeteer = require("puppeteer");

  const website = "https://facebook.com/";

  puppeteer
    .launch({ headless: false })
    .then(async (browser) => {
      const page = await browser.newPage();
      await page.goto(website);
     
      const context = browser.defaultBrowserContext();
      context.overridePermissions(website, ["notifications"]);
    })
    .catch(console.log);

Upvotes: 0

Muhammad Qasim
Muhammad Qasim

Reputation: 121

You just have to pass args

Example:

args: [
        "--disable-notifications"
      ]

Upvotes: 10

user8758751
user8758751

Reputation:

Yes you can overide notifications as @splintor said. This is the code that would, for example, disable the Allow Notifications popup when logging into facebook.

let crawl = async function(){

    let browser = await puppeteer.launch({ headless:false });
    const context = browser.defaultBrowserContext();
                              //        URL                  An array of permissions
    context.overridePermissions("https://www.facebook.com", ["geolocation", "notifications"]);
    let page = await browser.newPage();
    await page.goto("https://www.facebook.com");

    await page.type("#email", process.argv[2]);
    await page.type("#pass", process.argv[3]);
    await page.click("#u_0_2");
    await page.waitFor(1000);
    await page.waitForSelector("#pagelet_composer");
    let content2 = await page.$$("#pagelet_composer");
    console.log(content2); // .$$ An array containing elementHandles .$ would return 1 elementHandle

}

crawl();

Upvotes: 11

splintor
splintor

Reputation: 10154

Check ovverridePermissions, which was added in Puppeteer v1.8.0.

Upvotes: 3

Related Questions