DEV Community

Julia Theron
Julia Theron

Posted on

Testing Search Results from Different Locations Without a VPN

I recently had to audit a client\'s SEO performance across 15 different cities in 5 countries. Manually changing VPN locations was driving me insane.

So I wrote a Node.js script that uses headless Chrome to simulate searches from specific locations. The trick is overriding the navigator.geolocation API before the page loads:

javascript
const puppeteer = require(\'puppeteer\');

async function getLocalResults(query, lat, lng) {
const browser = await puppeteer.launch();
const page = await browser.newPage();

// Override geolocation
await page.setGeolocation({ latitude: lat, longitude: lng });

await page.goto(https://www.google.com/search?q=${encodeURIComponent(query)});

// Extract local pack results
const localResults = await page.evaluate(() => {
const items = document.querySelectorAll(\'.local-pack-item\');
return Array.from(items).map(item => item.innerText);
});

console.log(localResults);
await browser.close();
}

getLocalResults(\'pizza delivery\', 40.7128, -74.0060); // NYC

This worked, but maintaining it for multiple cities was a pain. I ended up switching to a service that does all this natively. Now I just pass a location and get clean, consistent results every time.

https://serpspur.com

Top comments (0)