我想通过Chromedriver功能自动授予对Chrome中的视频和音频的访问权限。
基于这个(相当旧的)答案,我尝试了以下操作:
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);
关于如何正确授予权限的任何想法?
面对类似的问题,此问题得以解决:
ChromeOptions options = new ChromeOptions(); options.addArguments("--use-fake-ui-for-media-stream=1"); driver = new ChromeDriver(options);
您也可以使用以下方法更改默认摄像机:
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);
相机代码可以从设置窗口(使用开发工具进行检查)或从Chrome目录中的首选项文件获得。