如何允许 Chrome 以编程方式使用我的麦克风?

How do I allow Chrome to use my microphone programmatically?

我目前正在尝试 运行 使用 webdriverjs 和 chromedriver 进行的一些测试,但它们需要麦克风权限。

这是显示的弹出窗口:

我试过:

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

但是没用。

我也试过了

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

也没用!我猜这不是普通的警报。

有用的链接:

Chrome startup arguments list

Chrome options for java and ruby

Chromedriver github

如何以编程方式授予他们权限?

是否有任何标志或其他解决方法?

每次 运行 selenium 时都会加载一个新的配置文件,因此您对首选项和网站权限所做的更改不会在会话之间保留。要修改它,我们需要告诉 selenium 要加载哪个配置文件。

第 1 步。找到您的 Chrome 首选项文件:www.forensicswiki.org/wiki/Google_Chrome#Configuration

步骤 2. 将文件夹 Default 复制到某处。我假设它被复制到 /some/path/allow-mic/Default.

备选步骤 3(这更容易): 在复制之前 Default 使用 Chrome 访问 localhost:1337 并将麦克风设置为始终允许。

第 3 步。编辑 allow-mic/Default/Preferences,找到标签 "profile""content_settings""exceptions" 并添加

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

"exceptions"。你应该以这样的方式结束:

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

第 4 步: 配置 selenium 以使用已编辑的首选项:

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

您可以通过打开 chrome://version/.

检查是否正在使用正确的首选项集(配置文件路径)

您可以通过为 chromedriver 提供 hardware.audio_capture_allowed_urls 首选项来将 url 列入音频捕获白名单。

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

有点晚了,但在这里粘贴了如何为寻找相同内容的其他人执行此操作。

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

对于那些使用 Python 的人来说,这对我有用:

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)

同样,用于 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)

如果你想使用麦克风,

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)