Search code examples
javascriptnode.jswebautomationplaywright

Playwright close unnumbered page


I use code

const {chromium} = require('playwright');
(async () => {
    const userDataDir = '\NewData';
    const browser = await chromium.launchPersistentContext(userDataDir,{headless:false});
    const page = await browser.newPage();
    await page.goto('https://www.google.com/')
})()

But the browser runs one blank tab before opening google, I think this is because I wrote the cookies incorrectly, but I have no desire to rewrite them. How can I close the first tab?


Solution

  • Saving the context won't prevent the browser from leaving that empty tab. If you don't want to see it you can use it instead of creating a new one.

    const context = await chromium.launchPersistentContext(userDataDir,{headless:false});
    const [ page ] = context.pages();
    await page.goto('https://www.google.com/')