最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Puppeteer: Close geolocation permission request - Stack Overflow

programmeradmin1浏览0评论

How do I use Puppeteer to close Chrome's geolocation permission request? I've placed page.on everywhere, and tried using confirm, dialog, alert and prompt. I've also read the following links, but couldn't find a solution:

  • .12.0/docs/api.md#class-dialog
  • .12.0/docs/api.md#event-dialog
  • Puppeteer confirm
  • Puppeteer opening chrome instance for each file at once

My script:

console.log("\nProcess Started");
//sample webpage
let webPage = "/?location=us";

const puppeteer = require("puppeteer");

async function main() {
  const browser = await puppeteer.launch({
    headless: false
  });
  const page = await browser.newPage();
  await page.setViewport({
    width: 1600,
    height: 900
  });

  try {
    await page.goto(webPage);
    await page.waitForSelector("body");
    await console.log("Loaded: " + webPage);
  } catch (err) {
    console.log("Error: " + webPage + "\n" + err);
  }

  await page.waitFor(10000);

  browser.close();
  await console.log("Process Ended \n");
};
main();

How do I use Puppeteer to close Chrome's geolocation permission request? I've placed page.on everywhere, and tried using confirm, dialog, alert and prompt. I've also read the following links, but couldn't find a solution:

  • https://github.com/GoogleChrome/puppeteer/issues/1213
  • https://github.com/GoogleChrome/puppeteer/issues/1475
  • https://github.com/GoogleChrome/puppeteer/blob/v0.12.0/docs/api.md#class-dialog
  • https://github.com/GoogleChrome/puppeteer/blob/v0.12.0/docs/api.md#event-dialog
  • Puppeteer confirm
  • Puppeteer opening chrome instance for each file at once
  • https://gist.github.com/mbierman/5b3e671fa4e848eec899ff486d0cdc26

My script:

console.log("\nProcess Started");
//sample webpage
let webPage = "https://www.mta.org.nz/find-an-mta/?location=us";

const puppeteer = require("puppeteer");

async function main() {
  const browser = await puppeteer.launch({
    headless: false
  });
  const page = await browser.newPage();
  await page.setViewport({
    width: 1600,
    height: 900
  });

  try {
    await page.goto(webPage);
    await page.waitForSelector("body");
    await console.log("Loaded: " + webPage);
  } catch (err) {
    console.log("Error: " + webPage + "\n" + err);
  }

  await page.waitFor(10000);

  browser.close();
  await console.log("Process Ended \n");
};
main();
Share Improve this question edited Jul 9, 2018 at 17:16 tony19 138k23 gold badges277 silver badges346 bronze badges asked Jul 8, 2018 at 6:14 KevinKevin 2891 gold badge4 silver badges13 bronze badges 2
  • I don't see a dialog on that page. I see a geolocation permissions request from Chrome though. If that was what you're referring to, it's not yet supported. But you might want to try patching navigator.getlocation as a workaround. – tony19 Commented Jul 8, 2018 at 6:50
  • Yes that is what I am referring to. I did not realize it was something different. Will try your suggestion once I've learnt how to implement them. I figured I was doing something fundamentally wrong. – Kevin Commented Jul 8, 2018 at 7:43
Add a comment  | 

3 Answers 3

Reset to default 6

The Puppeteer API (v1.5.0) currently does not support catching permission requests (which would've allowed granting/denying the geolocation permission), but as a workaround, you could patch navigator.geolocation in page.evaluateOnNewDocument():

await page.evaluateOnNewDocument(function() {
  navigator.geolocation.getCurrentPosition = function (cb) {
    setTimeout(() => {
      cb({
        'coords': {
          accuracy: 21,
          altitude: null,
          altitudeAccuracy: null,
          heading: null,
          latitude: 23.129163,
          longitude: 113.264435,
          speed: null
        }
      })
    }, 1000)
  }
});

await page.goto('https://www.mta.org.nz/find-an-mta/?location=us')

demo

// page.setGeolocation(options)

const context = browser.defaultBrowserContext();
await context.overridePermissions(url, ['geolocation']);

https://pptr.dev/#?product=Puppeteer&version=v1.18.1&show=api-browsercontextoverridepermissionsorigin-permissions

This is what worked with "headless: false" but could not get it to work "headless: true".

  1. Added " --user-data-dir=C:/Users/[USER]/PATH/TO/MY/DIRECTORY/myUserDataDir " to the command line(Read Note 1)
  2. Added " userDataDir: './myUserDataDir' " to my script in puppeteer.launch().
  3. Set as " headless: false " in puppeteer.launch().
  4. Run script from command line. " node C:/Users/[USER]/PATH/TO/MY/DIRECTORY/script.js --user-data-dir=C:/Users/[USER]/PATH/TO/MY/DIRECTORY/myUserDataDir "(Read Note 1)
  5. Chromium open's, page loads and when popup opens. Click popup button and then close the browser. Selection is now saved
  6. The next time the script is run the popup will not open and the page will finish loading.(Read Note 2)

Note 1 : --user-data-dir= in command line is recommended but does not seem to have any effect. This script works without it.

Note 2 : I cannot get this to work in headless mode. It hangs! I suspect something needs to be manually changed in myUserDataDir files. Does anyone know?

console.log("\nProcess Started");

//sample webpage
let webPage = "https://www.mta.org.nz/find-an-mta/?";

const puppeteer = require("puppeteer");

async function main() {
    const browser = await puppeteer.launch({
        userDataDir: "C:/Users/[USER]/PATH/TO/MY/DIRECTORY/myUserDataDir", //New line added
        headless: false
        });
    const page = await browser.newPage();
    await page.setViewport({width: 1600, height: 900});

    try{
        await page.goto(webPage); 
        await page.waitForSelector("body"); 
        await console.log("Loaded: " + webPage);
    }
    catch(err){             
        console.log("Error: " + webPage + "\n" + err);          
    }

    await page.waitFor(10000);

    browser.close();
    await console.log("Process Ended \n");  
};
main();
发布评论

评论列表(0)

  1. 暂无评论