[英]Puppeteer how to goto directly desired page
const puppeteer = require('puppeteer');
(async () => {
// 1. Launch the browser
const browser = await puppeteer.launch({
"args": [
'--remote-debugging-port=9222'
],
"headless": false,
});
// Open a new page
const page = await browser.newPage();
// Set UserAgent to Firefox
await page.setUserAgent('Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/66.0.3359.139 Safari/537.36');
await page.setBypassCSP(true);
// Navigate to URL
await page.goto('https://www.google.com');
When I run the script above, it opens a tab about:blank
then it opens another tab with google.com
当我运行上面的脚本时,它会打开一个选项卡
about:blank
然后它会打开另一个带有google.com
的选项卡
How can I only open the google page?我怎样才能只打开谷歌页面?
Or make it go to google page
from the about:blank page
.或者从
about:blank page
将其设置为 go 到google page
页面。
When you call the puppeteer.launch
it opens up a page automatically.当您调用
puppeteer.launch
时,它会自动打开一个页面。 Next when you call browser.newPage
, this gives you another page (tab).接下来,当您调用
browser.newPage
时,这将为您提供另一个页面(选项卡)。
If you do not need an additional page, what you could do is use the one that got opened as part of puppeteer.launch
.如果您不需要额外的页面,您可以使用作为
puppeteer.launch
的一部分打开的页面。 Something like:就像是:
// 1. Launch the browser
const browser = await puppeteer.launch({
"args": [
'--remote-debugging-port=9222'
],
"headless": false,
});
// 2. Get the default page
const pages = await browser.pages();
const page = pages[0];
//3. Use this page object subsequently
await page.goto('https://www.google.com');
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.