Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Accept permission request in chrome using selenium

I have a HTML/Javascript file with google's web speech api and I'm doing testing using selenium, however everytime I enter the site the browser requests permission to use my microphone and I have to click on 'ALLOW'.

How do I make selenium click on ALLOW automatically ?

like image 483
Shady Programmer Avatar asked Feb 07 '14 13:02

Shady Programmer


People also ask

Can you use Selenium with requests?

Extends Selenium WebDriver classes to include the request function from the Requests library, while doing all the needed cookie and request headers handling.

How do I handle permission pop-ups?

Handling permission pop-ups. While testing your mobile app, it may ask for specific permissions during the test. Various system dialogs or popups may appear, asking the user to grant access. The mobile app might ask the user permission to access the contacts, notifications, media, ... on the device.

How do I add Chrome options to Selenium?

Creating an instance of ChromeOptions class: See below code: ChromeOptions options = new ChromeOptions(); options. addArguments("disable-infobars"); ChromeDriver driver = new ChromeDriver(options);


2 Answers

Wrestled with this quite a bit myself.

The easiest way to do this is to avoid getting the permission prompt is to add --use-fake-ui-for-media-stream to your browser switches.

Here's some shamelessly modified code from @ExperimentsWithCode's answer:

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(executable_path="path/to/chromedriver", chrome_options=chrome_options)
like image 187
sokkyoku Avatar answered Sep 24 '22 21:09

sokkyoku


So I just ran into another question asking about disabling a different prompt box. It seems there may be a way for you to accomplish your goal.

This page lists options for starting chrome. One of the options is

--disable-user-media-security

"Disables some security measures when accessing user media devices like webcams and microphones, especially on non-HTTPS pages"

So maybe this will work for you:

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

chrome_options = Options()
chrome_options.add_argument("--disable-user-media-security=true")

driver = webdriver.Chrome(executable_path="path/to/chromedriver", chrome_options=chrome_options)
like image 29
ExperimentsWithCode Avatar answered Sep 24 '22 21:09

ExperimentsWithCode