What is the Puppeteer method to override the geolocation coordinates?
Antek N
antek n profile pic

The Puppeteer method to override the geolocation coordinates ispage.setGeolocation(). Here's a detailed explanation: 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. Overriding geolocation coordinates usingpage.setGeolocation(): To override the geolocation coordinates, you can use thepage.setGeolocation() method.

1
2
3
4
5
6

   await page.setGeolocation({ latitude: 37.7749, longitude: -122.4194 });

   // Proceed with page navigation or other actions that require geolocation
   await page.goto('https://example.com');
   

In this example,page.setGeolocation() is used to set the latitude and longitude coordinates to the desired values. In this case, the coordinates correspond to San Francisco. After setting the geolocation, you can proceed with page navigation or any other actions that rely on geolocation. By utilizing thepage.setGeolocation() method, you can override the geolocation coordinates in Puppeteer. This allows you to simulate different geographic locations during automated testing, geolocation-based services, or any other scenarios where emulating specific geolocation data is required.