简体   繁体   English

Puppeteer 如何直接转到所需的页面

[英]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.

相关问题 Puppeteer 无法转到 web 页面获取选择器 - Puppeteer cannot goto web page to get selector Puppeteer page.goto(url) 内部循环不等到加载 - Puppeteer page.goto(url) inside looping not waiting until load Puppeteer 在测试中不会转到 url(如何清除会话存储“sessionStorage 未定义”) - Puppeteer will not goto url in test (How to clear session storage 'sessionStorage is not defined') 如何在Puppeteer中重新加载页面? - How to reload page in Puppeteer? 在 puppeteer/JavaScript 中重试 page.goto、page.waitForNavigation 等的最佳实践 - Best Practice for retrying page.goto, page.waitForNavigation etc. in puppeteer/JavaScript 我不能使用page.goto() - Puppeteer从一个页面转到另一个页面 - I can't go from a page to another using page.goto() - Puppeteer URL字符串中最多可以传递给Puppeteer page.goto(url)函数的字符是什么 - What are the max chars in a URL string that can be passed to the Puppeteer page.goto(url) function 在 Puppeteer 中如何从默认配置文件切换到 chrome 窗口到所需的配置文件 - In Puppeteer how to switch to chrome window from default profile to desired profile 让 Puppeteer 同时转到不同的链接 - Making Puppeteer goto different links at the same time Node js Puppeteer goto页面数组 - Node js Puppeteer goto array of pages
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM