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

时间:2016-08-08 15:10:49

标签: javascript google-chrome mocha selenium-chromedriver webdriverjs

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

这是显示的弹出窗口:

popup image

我试过了:

    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

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

周围是否有任何旗帜或其他方式?

6 个答案:

答案 0 :(得分:7)

有点晚了,但是在这里为其他人寻找同样的事情,如何做到这一点。

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

答案 1 :(得分:5)

每次运行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/来检查是否正在使用正确的偏好设置(配置文件路径)。

答案 2 :(得分:1)

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

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

答案 3 :(得分:1)

对于那些使用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)

答案 4 :(得分:0)

类似地,用于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)

答案 5 :(得分:0)

如果您想使用麦克风,

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)