-1

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.

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.

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:

how to allow mic/camera in edge web browse suing selenium

How to enable 'Mircophone' access in Edge browser using Selenium?

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.

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

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.

Jamie Scott
  • 452
  • 4
  • 20
  • you could try using "options.setUnhandledPromptBehaviour("accept"), but it looks like it's only partially supported at this time: https://learn.microsoft.com/en-us/microsoft-edge/webdriver I'm not sure what the actual flag would be or the syntax in python... – pcalkins Oct 18 '19 at 19:33
  • I hate to ask a question here, but does "dom.webnotifications.enabled" actually work when setting it to false? (Just curious because I don't have access to an Edge browser at the moment...) – pcalkins Oct 18 '19 at 19:35
  • @pcalkins I've just tried that and have had no luck. Looks like it's just a regular capability (https://www.w3.org/TR/webdriver/#capabilities) so I added it as so. `options.set_capability("unhandledPromptBehavior", "accept")` – Jamie Scott Oct 21 '19 at 10:11

2 Answers2

0

I have reproduced the problem on my machine, but I think this is a Windows behavior, and we can't set using selenium.

From this article, we can see that:

How to allow a website to use your camera or microphone while browsing in Microsoft Edge

You can use your camera and microphone for websites in Microsoft Edge. However, even when your camera and microphone are enabled for Microsoft Edge, you will still need to give individual websites permission before they can use your camera and microphone. Here’s how:

  1. Go to a website that wants to use your microphone and/or camera.
  2. If a dialog box appears asking if you want to give the website permission to use your camera or microphone, select Allow once or Always allow, or close the dialog box to block access.

After allowed the permission, we can find this permission under the Manage permissions (Edge browser setting => Advanced => Manage permissions), screenshot as below:

enter image description here

From your description, when Edge browser exit, you will clear sessions, cookie and cache, if you don't want to allow, the permission every time, you could uncheck the Website Permissions option (screenshot as below), the permission prompts will only show when we first visit the page.

enter image description here

Zhi Lv
  • 18,845
  • 1
  • 19
  • 30
  • Hmm, I thought about this but I have about 50 domains to test against, which all need this permission (that's a lot of clicking), and they do change from time to time. Do you know if there's a way to set the allowed domains in advance? In a settings file or something – Jamie Scott Oct 21 '19 at 10:09
  • At present, I'm not finding there to have a setting file to set the site permission. As the above reply said, the only way I found is to manage the site permission is using the Manage permissions (Edge browser setting => Advanced => Manage permissions) I will try to submit the feedback regarding this issue. If I get any solution or a work around then I will try to provide you in the future. Thanks for your understanding. – Zhi Lv Oct 22 '19 at 08:52
  • I have found a solution to automatically add domains to Edge's allowed list, see my answer to this question. – Jamie Scott Oct 25 '19 at 09:07
0

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.

The registry path you want is

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

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

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.

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)

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.

Jamie Scott
  • 452
  • 4
  • 20