Grant access to video and audio in Chrome 48 via Selenium Chromedriver automatically

本秂侑毒 提交于 2019-12-29 09:14:37

问题


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?


回答1:


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.




回答2:


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);



回答3:


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.



来源:https://stackoverflow.com/questions/35067298/grant-access-to-video-and-audio-in-chrome-48-via-selenium-chromedriver-automatic

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!