How do I allow Chrome to use my microphone programmatically?
Asked Answered
M

6

19

I am currently trying to run some tests made with webdriverjs and chromedriver but they need microphone permissions.

This is the popup that shows up:

popup image

I have tried:

    chromedriver.start(['--disable-popup-blocking']);
    driver = new Webdriver.Builder()
    .withCapabilities(Webdriver.Capabilities.chrome())
    .build();

but it didn't work.

I also tried

    driver.wait(Until.alertIsPresent(), config.TIMEOUT, 'Alert did not show up');
    driver.switchTo().alert().accept();

it did not work either! I guess that this is not an ordinary alert.

Useful links:

Chrome startup arguments list

Chrome options for java and ruby

Chromedriver github

How do I give them permissions programmatically?

Is there any flag or some other way around this?

Monique answered 8/8, 2016 at 15:10 Comment(9)
This is security thing and most probably Google Chrome will not allow to circumvent it. On https: pages you have to give permission only once.Coccid
pfffff, thanks for the tip.Deliquesce
@AlexKudryashev, as a matter of fact, Chrome has disabled accessing userMedia, microphone, geoLocation, and most APIs on non-SSL domain. localhost is considered as a secure domain though (for development purposes).Borax
Well it should stop asking for mic permissions then... xDDeliquesce
Check site's setting (lock icon on left of address bar) screenshotBorax
It asks for permission on each request on http: pages and once per domain on https:pages. If user click Block then microphone will be blocked for this domain. This is default behavior.Coccid
--use-fake-device-for-media-stream did not work eitherDeliquesce
Do not forget that chrome is launched as "brand new". No settings, extensions etc.Deliquesce
Adam even if i set "Always allow" it will only set it 1 time.Deliquesce
D
7

A fresh profile is loaded each time you run selenium, hence changes you make to the preferences and website permissions are not preserved between sessions. To amend this we need to tell selenium which profile to load.

Step 1. Find your Chrome preferences file: www.forensicswiki.org/wiki/Google_Chrome#Configuration

Step 2. Copy the folder Default somewhere. I will assume it is copied to /some/path/allow-mic/Default.

Alternative Step 3 (this is easier): Before copying Default visit localhost:1337 with Chrome and set mic to always allow.

Step 3. Edit allow-mic/Default/Preferences, find the tags "profile", "content_settings" and "exceptions" within each other and add

"media_stream_mic":{"http://localhost:1337,*":
                                          {"last_used":1470931206,
                                           "setting":1} },

to "exceptions". You should end up with something like:

...
"profile":{
     ...
     "content_settings": {
         ...
         "exceptions": {
             ...
             "media_stream_mic":{"http://localhost:1337,*":
                                      {"last_used":1470931206,
                                       "setting":1} },
             ...
         },
    },
},
...

Step 4: Configure selenium to use the edited preferences:

var chromedriver = require('chromedriver');
var Webdriver = require('selenium-webdriver');
var chrome = require('selenium-webdriver/chrome');

var opts = new chrome.Options();                   
opts.addArguments("user-data-dir=/some/path/allow-camera");

var driver = new chrome.Driver(opts);

You can check that the correct set of preferences (Profile path) are in use by opening chrome://version/.

Dochandorrach answered 11/8, 2016 at 16:34 Comment(3)
Looks nice, i will have a try with this later. I have to find ChromeOptions inside webdriverjs though.Deliquesce
I was not able to use this tip. I setted the path to a folder that had a Preferences file that had the following settings: "media_stream_mic":{"localhost:1337,":{"last_used":1473864155.757848,"setting":1},"localhost:1338,":{"last_used":1470767319.186194,"setting":1},...} When i run selenium, it still asks for mic permissions.Deliquesce
Could you try to add "http://...,*" to your urls, so that they match the urls in the answer? Have you tried using the alternative Step 3? Have you checked chrome://version to confirm that you are using the correct set of preferences?Dochandorrach
N
14

A little late but pasting how to do this here for others looking for the same.

const webdriver = require('selenium-webdriver'), By = webdriver.By, until = webdriver.until,Builder= webdriver.Builder;

var chrome = require('selenium-webdriver/chrome');

var chromeOptions = new chrome.Options()
.addArguments('allow-file-access-from-files')
.addArguments('use-fake-device-for-media-stream')
.addArguments('use-fake-ui-for-media-stream');

var driver = new webdriver.Builder()
.forBrowser('chrome')
.setChromeOptions(chromeOptions);

driver = driver.build();
Neoptolemus answered 27/1, 2017 at 22:24 Comment(2)
Thanks - just 'use-fake-ui-for-media-stream' was enough to fix my mic permission problem.Xeroderma
Awesome! Also worked in Java Selenium: ChromeOptions options = new ChromeOptions(); options.addArguments("use-fake-device-for-media-stream"); options.addArguments("use-fake-ui-for-media-stream"); driver = new ChromeDriver(options);Oligochaete
D
7

A fresh profile is loaded each time you run selenium, hence changes you make to the preferences and website permissions are not preserved between sessions. To amend this we need to tell selenium which profile to load.

Step 1. Find your Chrome preferences file: www.forensicswiki.org/wiki/Google_Chrome#Configuration

Step 2. Copy the folder Default somewhere. I will assume it is copied to /some/path/allow-mic/Default.

Alternative Step 3 (this is easier): Before copying Default visit localhost:1337 with Chrome and set mic to always allow.

Step 3. Edit allow-mic/Default/Preferences, find the tags "profile", "content_settings" and "exceptions" within each other and add

"media_stream_mic":{"http://localhost:1337,*":
                                          {"last_used":1470931206,
                                           "setting":1} },

to "exceptions". You should end up with something like:

...
"profile":{
     ...
     "content_settings": {
         ...
         "exceptions": {
             ...
             "media_stream_mic":{"http://localhost:1337,*":
                                      {"last_used":1470931206,
                                       "setting":1} },
             ...
         },
    },
},
...

Step 4: Configure selenium to use the edited preferences:

var chromedriver = require('chromedriver');
var Webdriver = require('selenium-webdriver');
var chrome = require('selenium-webdriver/chrome');

var opts = new chrome.Options();                   
opts.addArguments("user-data-dir=/some/path/allow-camera");

var driver = new chrome.Driver(opts);

You can check that the correct set of preferences (Profile path) are in use by opening chrome://version/.

Dochandorrach answered 11/8, 2016 at 16:34 Comment(3)
Looks nice, i will have a try with this later. I have to find ChromeOptions inside webdriverjs though.Deliquesce
I was not able to use this tip. I setted the path to a folder that had a Preferences file that had the following settings: "media_stream_mic":{"localhost:1337,":{"last_used":1473864155.757848,"setting":1},"localhost:1338,":{"last_used":1470767319.186194,"setting":1},...} When i run selenium, it still asks for mic permissions.Deliquesce
Could you try to add "http://...,*" to your urls, so that they match the urls in the answer? Have you tried using the alternative Step 3? Have you checked chrome://version to confirm that you are using the correct set of preferences?Dochandorrach
S
5

For those using Python, this worked for me:

from selenium import webdriver
from selenium.webdriver.chrome.options import Options

chrome_options = Options()
chrome_options.add_argument("--use-fake-ui-for-media-stream")
driver = webdriver.Chrome(chrome_options=chrome_options)
Siphon answered 9/11, 2017 at 9:31 Comment(0)
S
3

You can whitelist a url for audio-capture by providing chromedriver with the hardware.audio_capture_allowed_urls preference.

...
chrome_options = Options()
prefs = {"hardware.audio_capture_allowed_urls" : ["example.org"]}
chrome_options.add_experimental_option("prefs",prefs)
driver = webdriver.Chrome(chrome_options=chrome_options)
Stonefly answered 12/12, 2016 at 9:55 Comment(0)
E
0

Similarly, for use in Splinter

    from splinter import Browser
    from selenium.webdriver.chrome.options import Options 
    chrome_options = Options() 
    chrome_options.add_argument("--use-fake-ui-for-media-stream") 
    Browser('chrome', ** {'executable_path':'chromedriver'},options=chrome_options)
Electromotor answered 4/6, 2020 at 23:12 Comment(0)
F
0

If you want to use the microphone,

chrome_options = Options()

chrome_options.add_experimental_option('prefs',{'profile.default_content_setting_values.media_stream_mic':1})


driver = webdriver.Chrome(path,chrome_options=chrome_options)
Flamethrower answered 17/3, 2021 at 11:41 Comment(1)
Add some description to your code, it will help understand better.Growl

© 2022 - 2024 — McMap. All rights reserved.