Mock GeoLocation using Selenium 4

Dheeraj Gambhir
1 min readMay 16, 2020

If there is a use case where you need to override the geolocation of the browser before you hit the website, Selenium 4 Alpha version does support that now by using the power of Chrome DevTools and Protocol (CDP) along with setGeolocationOverride method present in Emulation class.

To set it back to default, use the clearGeolocationOverride method.

@Test

private void testGeo(){

driver.get(“https://www.google.com/maps?q=28.704060,77.102493");

Builder<String, Object> mapLatLan = new ImmutableMap.Builder<String, Object>();

mapLatLan.put(“latitude”, 40.712776);

mapLatLan.put(“longitude”, -74.005974);

mapLatLan.put(“accuracy”, 100);

((ChromeDriver) driver).executeCdpCommand(“Emulation.setGeolocationOverride”, mapLatLan.build());

driver.get(“https://www.google.com/maps?q=40.712776,-74.005974");

}

GeoLocation

--

--