1

I would like to automatically grant access to video and audio in Chrome via Chromedriver capabilities.

Based on this (pretty old) answer I tried the following:

DesiredCapabilities capabilities = DesiredCapabilities.chrome();
ChromeOptions options = new ChromeOptions();
Map<String, Object> prefs = new HashMap<>();

// with this chrome still asks for permission
prefs.put("profile.managed_default_content_settings.media_stream", 1);
prefs.put("profile.managed_default_content_settings.media_stream_camera", 1);
prefs.put("profile.managed_default_content_settings.media_stream_mic", 1);

// and this prevents chrome from starting
prefs.put("profile.content_settings.exceptions.media_stream_mic.https://*,*.setting", 1);
prefs.put("profile.content_settings.exceptions.media_stream_mic.https://*,*.last_used", 1);
prefs.put("profile.content_settings.exceptions.media_stream_camera.https://*,*.setting", 1);
prefs.put("profile.content_settings.exceptions.media_stream_camera.https://*,*.last_used", 1);

// and this prevents chrome from starting as well
prefs.put("profile.content_settings.pattern_pairs.https://*,*.media_stream.video", "Allow");
prefs.put("profile.content_settings.pattern_pairs.https://*,*.media_stream.audio", "Allow");

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

Any ideas on how to grant permissions correctly?

Community
  • 1
  • 1
Yser
  • 2,086
  • 22
  • 28

3 Answers3

3

Faced similar issue, solved with this:

ChromeOptions options = new ChromeOptions();
options.addArguments("--use-fake-ui-for-media-stream=1");
driver = new ChromeDriver(options);

You can also change default camera using this method:

Map<String, Object> prefs = new HashMap<String, Object>();
prefs.put("media.default_video_capture_Device", "\\\\?\\root#media#0002#{65e8773d-8f56-11d0-a3b9-00a0c9223196}\\global");
options.setExperimentalOption("prefs", prefs);

Camera code could be obtained from settings window (inspect with dev tools) or from preferences file in Chrome directory.

JackHammer
  • 458
  • 1
  • 3
  • 16
1

There is a security limitation because of which media preferences cannot be set by default. However, we have a workaround by enabling the required settings in preferences (As mentioned in commen#1) 1. Run chrome manually with "/path/to/chrome --user-data-dir=/give/some/path/for/profileDir". 2. Go to your required URL where it shows Video allow or block popup message 3. Click on Allow button 4. Go to chrome://settings/content -> click on Manage exception button under Media section -> Here, you will find that the URL has been added to the manage exception box 5. Now, pass this user data directory to chromedriver with the use of below code

Java Code -

ChromeOptions options = new ChromeOptions(); 
options.addArguments("user-data-dir=/path/to/the/saved/profileDir");
WebDriver driver = new ChromeDriver(options);
Sanju Abel
  • 111
  • 5
0

I think it needs to be site specific, the following works for me: prefs.put("profile.content_settings.exceptions.media_stream_camera.'https://somewebsite.com:443,'.setting", "1"); prefs.put("profile.content_settings.exceptions.media_stream_mic.'https://somewebsite.com:443,'.setting", "1");

Also, I think the website needs to be in single quotes or it won't parse right.

Give that a shot.

  • Nope - that wasn't what fixed it, I was running with a user-profile that had this already set: final ChromeOptions options = new ChromeOptions(); options.addArguments("--user-data-dir=/path/to/some/profile/that/allows/video"); – user3342560 Feb 26 '16 at 07:34