Mock GeoLocation using Selenium 4

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.


private void testGeo(){


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”,;