When using Puppeteer for browser automation, the browser runs by default in headless mode, meaning it has no graphical interface. If you need to open a blank tab in non-headless mode, you can follow these steps:
-
Set the browser to non-headless mode: When launching Puppeteer, set the
headlessoption tofalse. This allows the browser to run with a graphical interface. -
Open a new blank tab: Use Puppeteer's API to create a new browser page.
Here is a specific code example:
javascriptconst puppeteer = require('puppeteer'); (async () => { // Launch browser in non-headless mode const browser = await puppeteer.launch({ headless: false // Set to false to display the browser interface }); // Open a new blank page const page = await browser.newPage(); // Navigate to a blank page await page.goto('about:blank'); // You can add more operations here, such as taking screenshots or analyzing page content // Close the browser await browser.close(); })();
In this example, first, launch a non-headless browser instance using puppeteer.launch() with headless: false. Then, create a new page using browser.newPage(). Next, navigate to a blank page using page.goto('about:blank'). Finally, close the browser after completing the required operations.
Using non-headless mode provides the benefit of visually observing all browser activities, which is particularly useful for debugging or demonstrating automation scripts.