[英]How do I allow Chrome to use my microphone programmatically?
I am currently trying to run some tests made with webdriverjs and chromedriver but they need microphone permissions.我目前正在尝试运行一些使用 webdriverjs 和 chromedriver 进行的测试,但它们需要麦克风权限。
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 启动参数列表
Chrome options for java and ruby java 和 ruby 的 Chrome 选项
Chromedriver github Chromedriver github
How do I give them permissions programmatically?如何以编程方式授予他们权限?
Is there any flag or some other way around this?是否有任何标志或其他方式解决此问题?
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();
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.每次运行 selenium 时都会加载一个新的配置文件,因此您对首选项和网站权限所做的更改不会在会话之间保留。 To amend this we need to tell selenium which profile to load.
为了修改这个,我们需要告诉 selenium 加载哪个配置文件。
Step 1. Find your Chrome preferences file: www.forensicswiki.org/wiki/Google_Chrome#Configuration第 1 步。找到您的 Chrome 首选项文件: www.forensicswiki.org/wiki/Google_Chrome#Configuration
Step 2. Copy the folder Default
somewhere.步骤 2. 将文件夹
Default
复制到某处。 I will assume it is copied to /some/path/allow-mic/Default
.我假设它被复制到
/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.替代步骤 3(这更容易):在使用 Chrome 复制
Default
访问localhost:1337
并将麦克风设置为始终允许之前。
Step 3. Edit allow-mic/Default/Preferences
, find the tags "profile"
, "content_settings"
and "exceptions"
within each other and add步骤 3. 编辑
allow-mic/Default/Preferences
,在彼此中找到标签"profile"
、 "content_settings"
和"exceptions"
并添加
"media_stream_mic":{"http://localhost:1337,*":
{"last_used":1470931206,
"setting":1} },
to "exceptions"
.到
"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:第 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);
You can check that the correct set of preferences (Profile path) are in use by opening chrome://version/
.您可以通过打开
chrome://version/
来检查是否正在使用正确的首选项集(配置文件路径)。
For those using Python, this worked for me:对于那些使用 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)
You can whitelist a url for audio-capture by providing chromedriver with the hardware.audio_capture_allowed_urls preference.您可以通过向 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)
Similarly, for use in Splinter
同样,用于
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)
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)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.