How do I set a custom user agent in Puppeteer?Antek N
To set a custom user agent in Puppeteer, you can utilize thepage.setUserAgent()
method. Here's a detailed explanation of how to set a custom user agent:
1. Launching a new browser instance and creating a new page:
1 2 3 4 5 6 7 8 9 10 11 12 13
const puppeteer = require('puppeteer'); (async () => { const browser = await puppeteer.launch(); const page = await browser.newPage(); // Perform actions with the page here // Close the browser await browser.close(); })();
This code sets up a basic Puppeteer script. It launches a new headless browser instance and creates a new page to work with. 2. Setting a custom user agent:
1 2 3 4 5
const userAgent = 'Your Custom User Agent'; await page.setUserAgent(userAgent);
ThesetUserAgent()
method is used to set a custom user agent for the page. Provide your desired user agent string as a parameter to the method.
For example, you can set the user agent to mimic a specific browser and version:
1 2 3
await page.setUserAgent('Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/90.0.4430.212 Safari/537.36');
Or you can set a custom user agent to provide any other value you desire.
3. Verifying the user agent:
To confirm that the user agent has been set correctly, you can navigate to a page that displays user agent information and retrieve it usingpage.evaluate()
:
1 2 3 4 5 6
await page.goto('https://example.com'); const userAgent = await page.evaluate(() => window.navigator.userAgent); console.log('Current User Agent:', userAgent);
In this example, the page navigates tohttps://example.com
, and thenpage.evaluate()
is used to retrieve thewindow.navigator.userAgent
property, which represents the user agent. The user agent is then logged to the console for verification.
By following these steps, you can set a custom user agent in Puppeteer using thesetUserAgent()
method. This allows you to emulate different browsers, versions, or any desired user agent to suit your testing or scraping needs.
Similar Questions
How do I set cookies in Puppeteer?
How do I handle page timeouts in Puppeteer?
How do I wait for a page to fully load in Puppeteer?
How do I handle page navigation errors in Puppeteer?
How do I handle page redirects in Puppeteer?
How do I handle page authentication prompts in Puppeteer?
How do I handle dynamically generated content in Puppeteer?
How do I handle navigation events in Puppeteer?
How do I type text into an input field using Puppeteer?
How can I simulate touch events using Puppeteer?
How do I handle browser geolocation prompts in Puppeteer?
How do I handle page authentication using username and password in Puppeteer?
How do I handle slow loading elements in Puppeteer?
How do I simulate a mobile device in Puppeteer?
How do I handle AJAX requests in Puppeteer?
How can I get the current URL of a page using Puppeteer?
How can I extract data from a web page using Puppeteer?
How do I handle CAPTCHA challenges in Puppeteer?