如何使用Puppeteer关闭Chrome的地理位置许可请求?我已经将page.放置在任何地方,并尝试使用确认,对话框,警报和提示.我也阅读了以下链接,但找不到解决方案:
> 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
我的剧本:
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();
解决方法:
Puppeteer API(v1.5.0)当前为does not support catching permission requests(这将允许授予/拒绝地理位置许可),但作为解决方法,您可以在page.evaluateOnNewDocument()
中输入patch navigator.geolocation
:
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')