通过 Selenium Chrome 驱动程序自动授予对 Chrome 48 中的视频和音频的访问权限
Grant access to video and audio in Chrome 48 via Selenium Chromedriver automatically
我想通过 Chrome 驱动程序功能自动授予对 Chrome 中视频和音频的访问权限。
基于this(相当旧的)答案,我尝试了以下方法:
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);
关于如何正确授予权限有什么想法吗?
我认为它需要特定于站点,以下适用于我:
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");
此外,我认为该网站需要用单引号引起来,否则无法正确解析。
试一试。
遇到了类似的问题,解决了这个问题:
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);
可以从设置 window(使用开发工具检查)或 Chrome 目录中的首选项文件中获取相机代码。
存在安全限制,因为默认情况下无法设置媒体首选项。
但是,我们有一个解决方法,可以在首选项中启用所需的设置(如 commen#1 中所述)
1. 运行 chrome 手动使用“/path/to/chrome --user-data-dir=/give/some/path/for/profileDir”。
2. 转到您需要的 URL 显示视频允许或阻止弹出消息的地方
3.点击允许按钮
4. 转到 chrome://settings/content -> 单击媒体部分下的管理例外按钮 -> 在这里,您会发现 URL 已添加到管理例外框
5. 现在,使用下面的代码
将此用户数据目录传递给 chrome 驱动程序
Java代码-
ChromeOptions options = new ChromeOptions();
options.addArguments("user-data-dir=/path/to/the/saved/profileDir");
WebDriver driver = new ChromeDriver(options);
我想通过 Chrome 驱动程序功能自动授予对 Chrome 中视频和音频的访问权限。
基于this(相当旧的)答案,我尝试了以下方法:
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);
关于如何正确授予权限有什么想法吗?
我认为它需要特定于站点,以下适用于我: 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");
此外,我认为该网站需要用单引号引起来,否则无法正确解析。
试一试。
遇到了类似的问题,解决了这个问题:
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);
可以从设置 window(使用开发工具检查)或 Chrome 目录中的首选项文件中获取相机代码。
存在安全限制,因为默认情况下无法设置媒体首选项。 但是,我们有一个解决方法,可以在首选项中启用所需的设置(如 commen#1 中所述) 1. 运行 chrome 手动使用“/path/to/chrome --user-data-dir=/give/some/path/for/profileDir”。 2. 转到您需要的 URL 显示视频允许或阻止弹出消息的地方 3.点击允许按钮 4. 转到 chrome://settings/content -> 单击媒体部分下的管理例外按钮 -> 在这里,您会发现 URL 已添加到管理例外框 5. 现在,使用下面的代码
将此用户数据目录传递给 chrome 驱动程序Java代码-
ChromeOptions options = new ChromeOptions();
options.addArguments("user-data-dir=/path/to/the/saved/profileDir");
WebDriver driver = new ChromeDriver(options);