-->

How do i allow Chrome to use my microphone program

2019-02-02 15:24发布

问题:

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:

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 programatically?

Is there any flag or some other way around this?

回答1:

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/.



回答2:

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();


回答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)


回答4:

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)