如何使用 Selenium 在 Edge 浏览器中启用媒体设备访问? [英] How to enable media device access in Edge browser using Selenium?

查看:154
本文介绍了如何使用 Selenium 在 Edge 浏览器中启用媒体设备访问?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 Selenium 测试,我想在 Edge 中针对使用网络摄像头和麦克风的页面运行.为了让这些媒体设备在浏览器中工作,用户必须允许站点访问设备.

I have a Selenium test I want to run in Edge against a page which uses the webcam and microphone. In order for those media devices to work in the browser, the user has to allow the site access to the device.

这是我制作的一个快速示例小提琴,它说明了 Edge 中的行为:https://jsfiddle.net/12t3nofL/

This is a quick example fiddle I made which illustrates that behaviour in Edge: https://jsfiddle.net/12t3nofL/

您应该注意到屏幕底部的弹出栏,我需要在我的自动化中自动允许它.

You should notice the popup bar at the bottom of the screen, I need this to be automatically allowed in my automation.

我已经在 SO 上发现了这些现有的 Q/As,这表明有一种方法可以通过 webdriver 选项允许媒体设备,但它们对我不起作用:

I found these existing Q/As on SO already which suggest there is a way to allow media devices via the webdriver options, but they just don't work for me:

如何允许麦克风/摄像头进入边缘网页浏览起诉 selenium

如何在 Edge 中启用麦克风"访问使用 Selenium 的浏览器?

这是我的代码(你可能需要先pip install selenium==3.141.0):

This is my code (you may need to pip install selenium==3.141.0 first):

from selenium import webdriver
from selenium.webdriver.edge.options import Options
options = Options()
options.set_capability("dom.webnotifications.enabled", 1)
options.set_capability("permissions.default.microphone", 1)
options.set_capability("permissions.default.camera", 1)
capabilities = options.to_capabilities()
driver = webdriver.Edge(capabilities=capabilities)
driver.get('https://jsfiddle.net/12t3nofL')

而且每次都会在底部弹出权限栏.

And the permissions bar still pops up at the bottom every time.

我使用的是 Selenium 3.141.0 和 Python 3.7.0(尽管在 2.7 中存在相同的问题).

I am on Selenium 3.141.0 and Python 3.7.0 (though have the same issue in 2.7).

注意:由于其他原因,我将 Edge 设置为在退出时清除会话,因此不会保留 cookie 和缓存 - 试图模仿您使用自动 Chrome 获得的新配置文件.

Note: due to other reasons, I have Edge set to clear sessions on exit so cookies and cache are not preserved - in an attempt to mimic the fresh profile you get with automated Chrome.

推荐答案

我找到了一个非常有效的解决方案.我可以将站点添加到 Edge 在注册表中允许的媒体设备站点列表中,而不是尝试单击按钮以允许媒体设备.然后,我几乎可以立即访问需要媒体设备的网站,并且不会弹出任何窗口.

I've found a solution which works quite well. Instead of trying to click the button to allow media devices, I can add the site to Edge's list of media devices allowed sites in the registry. I can then visit a site requiring media devices almost instantly after and no popup.

你想要的注册表路径是

HKEY_CURRENT_USER\SOFTWARE\Classes\Local Settings\Software\Microsoft\Windows\CurrentVersion\AppContainer\Storage\microsoft.microsoftedge_8wekyb3d8bbwe\MicrosoftEdge\MediaCapture\AllowDomains

这在我测试过的所有 Windows 10 机器上似乎都一样.

Which appears to be the same on all Windows 10 machines I test on.

在那里您要创建协议和 IP/FQDN 的 DWORD 键,例如http://10.0.0.2https://example.com,Edge 在允许域时设置的值为 3,所以我用那个.

In there you want to create a DWORD key of the protocol and IP/FQDN, e.g. http://10.0.0.2 or https://example.com, and the value Edge sets when you allow a domains is 3, so I use that.

在我的特殊情况下,我已切换到 IronPython,因此我可以轻松地连接到 Windows API 调用.这是我用来完成这项工作的代码片段.

In my particular case I've switched to IronPython so I can easily hook into Windows API calls. Here is a snippet of my code to make this work.

from Microsoft.Win32 import Registry

def add_edge_media_domain(domain):
    path = "HKEY_CURRENT_USER\\SOFTWARE\\Classes\\Local Settings\\Software\\Microsoft\\Windows\\CurrentVersion\\AppContainer\\Storage\\microsoft.microsoftedge_8wekyb3d8bbwe\\MicrosoftEdge\\MediaCapture\\AllowDomains"
    Registry.SetValue(path, domain, 3)

在我调用 driver.get(url) 之前,我调用了 add_edge_media_domain(trim_url(url)),其中 trim_url 只是切断了路径部分网址.

Before I call driver.get(url) I call add_edge_media_domain(trim_url(url)) where trim_url just cuts the path part off the URL.

这篇关于如何使用 Selenium 在 Edge 浏览器中启用媒体设备访问?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆