When automating browser tasks with Puppeteer, the puppeteer.launch() method plays a critical role in initializing a new browser instance. Understanding the different puppeteer args allows for fine-tuning this process, potentially improving both performance and security. This article provides a comprehensive guide on the puppeteer arguments and flags.
Table of Contents
Explanation Puppeteer.launch()
Below is a list of arguments that can be provided to the puppeteer.launch() method along with an explanation of each:
-
executablePath: Specify the path to a browser executable to run instead of the bundled Chromium.headless: Determines whether to run the browser in headless mode.args: A list of command-line arguments to pass to the browser instance.ignoreHTTPSErrors: Ignores HTTPS errors during navigation.
List of Puppeteer args
While Puppeteer’s default args are suitable for most use cases, fine-tuning your experience using additional args and flags can enhance performance or enable special features. Below is a comprehensive list of the most common Puppeteer args and flags:
-
--disable-software-rasterizer: Disable the software rasterizer.--disable-gpu: Disable GPU hardware acceleration.--disable-infobars: Disable the yellow infobar at the top of the browser.--disable-dev-shm-usage: Disable /dev/shm usage (for systems with low shared memory).--no-sandbox: Disable the sandbox for running in containerized environments.--disable-setuid-sandbox: Disable setuid sandbox (for Linux).--disable-web-security: Disable same-origin policy (not recommended for general use).--user-data-dir=/path/to/profile: Set the user data directory for the browser.--proxy-server=http://proxy.example.com:8080: Specify a proxy server.--proxy-bypass-list=<-loopback>: Bypass the proxy for local addresses.--disable-extensions: Disable browser extensions.--disable-plugins: Disable plugins.--incognito: Open a new browser window in incognito mode.--lang=en-US: Set the language for the browser.--headless: Run the browser in headless mode.--window-size=1200,800: Set the initial window size.--start-maximized: Start with the browser window maximized.--disable-popup-blocking: Disable the popup blocker.--disable-background-timer-throttling: Disable background timer throttling.--disable-backgrounding-occluded-windows: Disable backgrounding of occluded windows.--disable-breakpad: Disable crash reporting.--disable-client-side-phishing-detection: Disable client-side phishing detection.--disable-sync: Disable syncing with a Google Account.--disable-translate: Disable the built-in translation feature.--no-experiments: Disable experimental features.--remote-debugging-port=9222: Enable remote debugging on the specified port.--disable-features=site-per-process: Disable site isolation.
Implementing these arguments into your Puppeteer code is straightforward, as demonstrated in the provided code example with proper syntax.
const browser = await puppeteer.launch({
args: ['--no-sandbox', '--disable-infobars'],
});
const page = await browser.newPage();
// ... other Puppeteer operations
How to use Puppeteer args ?
const browser = await puppeteer.launch({
args: [
// Example additional command line arguments
'--disable-gpu', // Disable GPU hardware acceleration
'--disable-software-rasterizer', // Disable the software rasterizer
'--disable-infobars', // Disable the yellow infobar that appears on the top of the browser
'--disable-dev-shm-usage', // Disable /dev/shm usage (for systems with low shared memory)
'--no-sandbox', // Disable the sandbox for running in containerized environments
'--disable-setuid-sandbox',// Disable setuid sandbox (for Linux)
'--disable-web-security', // Disable same-origin policy (not recommended for general use)
'--user-data-dir=/path/to/profile', // Set the user data directory for the browser
'--proxy-server=http://proxy.example.com:8080', // Specify a proxy server
'--proxy-bypass-list=<-loopback>', // Bypass the proxy for local addresses
// Add any other desired command line arguments here
],
});
Conclusion
Leveraging Puppeteer args and flags can significantly modify the behavior of your headless browser sessions to match specific requirements. Remember to use them wisely, as they can affect security and performance. Always consult the official Puppeteer documentation for the latest and most comprehensive list of supported args and flags for your version of Puppeteer.
