LeBronPark
LeBronPark

Reputation: 15

How to add allow site on location of chrome content setting with selenium

I want to add site "a.com" on mobile chrome using selenium. Option - [Advanced-content setting-location-ask before accessing-allow site]

Because i want to rid of the popup on my testing

enter image description here

Does anyone know?

Upvotes: 1

Views: 7084

Answers (3)

linw1995
linw1995

Reputation: 188

Use chrome devtools protocol can do this.Browser.grantPermission allow to config the geolocation permission before accessing the target website. You can look at my another answer for more detail Setting sensors (location) in headless Chrome .

Upvotes: 1

jithinkmatthew
jithinkmatthew

Reputation: 930

The below code snippet working for me to disable that pop up. Instead of profile.default_content_settings.geolocation,

I used profile.default_content_setting_values.notifications.

ChromeOptions options = new ChromeOptions();

JSONObject jsonObject = new JSONObject();
jsonObject.put("profile.default_content_setting_values.notifications", 1);

options.setExperimentalOption("prefs", jsonObject);
WebDriver driver = new ChromeDriver(options);

or

DesiredCapabilities caps = new DesiredCapabilities();
ChromeOptions options = new ChromeOptions();

Map<String, Object> prefs = new HashMap<String, Object>();
prefs.put("profile.default_content_settings.popups", 0);
prefs.put("profile.default_content_setting_values.notifications", 1);

options.setExperimentalOption("prefs", prefs);
caps.setCapability(ChromeOptions.CAPABILITY, options);

Upvotes: 0

demouser123
demouser123

Reputation: 4264

To disable chrome asking for location you need to use ChromeOptions and disable geolocation from profile settings

 ChromeOptions options = new ChromeOptions();

 JSONObject jsonObject = new JSONObject();
 jsonObject.put("profile.default_content_settings.geolocation", 2);

 options.setExperimentalOption("prefs", jsonObject);
 WebDriver driver = new ChromeDriver(options);

Please see that the whole answer is already explained in this SO post.

Edit : In case, the option is to be kept enabled, you just need to change this line

jsonObject.put("profile.default_content_settings.geolocation", 2);

to

jsonObject.put("profile.default_content_settings.geolocation", 1);

Upvotes: 2

Related Questions