Shady Programmer
Shady Programmer

Reputation: 824

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 ?

Upvotes: 9

Views: 28291

Answers (9)

Sammi Voon
Sammi Voon

Reputation: 11

Thanks to @sokkyoku, I have tried many ways to allow my camera permission using selenium with node.js but I can't find any answers elsewhere.

For those who's using selenium with javascript like I do, here's the equivalent code to what @sokkyoku has provided:

const chrome = require ("selenium-webdriver/chrome");
const chromeOptions= new chrome.Options();
chromeOptions.addArguments('--use-fake-ui-for-media-stream');

driver = await new Builder().forBrowser("chrome").setChromeOptions(chromeOptions).usingServer("").build();

If you are not using selenium grid, you can just ignore the .usingServer("") part and it's good to go!

Upvotes: 0

Alex
Alex

Reputation: 203

Do it For android chrome it really work!

adb -s emulator-5554 push <YOU_COMPUTER_PATH_FOLDER>/com.android.chrome_preferences.xml  /data/data/com.android.chrome/shared_prefs/com.android.chrome_preferences.xml

File config here https://yadi.sk/d/ubAxmWsN5RQ3HA Chrome 80 x86

or you can save the settings file after ticking the box with your hands, in adb its "pull" - command

Upvotes: 0

sebpiq
sebpiq

Reputation: 7812

Building on the answer from @Shady Programmer.

I tried to send Tab keys with selenium in order to focus on the popup, but as reported by others it didn't work in Linux. Therefore, instead of using selenium keys, I use xdotool command from python :

def send_key(winid, key):
    xdotool_args = ['xdotool', 'windowactivate', '--sync', winid, 'key', key]
    subprocess.check_output(xdotool_args)

which for Firefox, gives the following approximate sequence :

# Focusing on permissions popup icon
for i in range(6):
    time.sleep(0.1)
    send_key(win_info['winid'], 'Tab')

# Enter permissions popup
time.sleep(0.1)
send_key(win_info['winid'], 'space')

# Focus on "accept" button
for i in range(3):
    time.sleep(0.1)
    send_key(win_info['winid'], 'Tab')

# Press "accept"            
send_key(win_info['winid'], 'a')

Upvotes: 1

Mykola
Mykola

Reputation: 417

Beware of Mohana Latha's answer for JAVA! The code is only pressing the buttons and NEVER releasing them. This will bring bunch of issues later on.

Use this instead:

    // opening new window
    Robot robot;
    try {
        robot = new Robot();
        robot.keyPress(KeyEvent.VK_CONTROL);
        robot.delay(100);
        robot.keyPress(KeyEvent.VK_N);
        robot.delay(100);
        robot.keyRelease(KeyEvent.VK_N);
        robot.delay(100);
        robot.keyRelease(KeyEvent.VK_CONTROL);
        robot.delay(100);
    } catch (AWTException e) {
        log.error("Failed to press buttons: " + e.getMessage());
    }

Upvotes: 1

Amit Saharan
Amit Saharan

Reputation: 73

You can allow using add_experimental_option as shown below.

from selenium.webdriver.chrome.options import Options
chrome_options = Options()
chrome_options.add_experimental_option('prefs',{'profile.default_content_setting_values.notifications':1})
driver = webdriver.Chrome(chrome_options=chrome_options)

Upvotes: 0

sokkyoku
sokkyoku

Reputation: 2221

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)

Upvotes: 16

Mohana Latha
Mohana Latha

Reputation: 1

[Java]: Yes there is a simple technique to click on Allow button using Robot-java.awt

public void allowGEOLocationCapture(){
    Robot robot = null;
    try {
        robot = new Robot();
        robot.keyPress(KeyEvent.VK_TAB);
        robot.keyPress(KeyEvent.VK_ENTER);
        robot.delay(600);
    } catch (AWTException e) {
       getLogger().info(e);
    }
 }

Upvotes: 0

Shady Programmer
Shady Programmer

Reputation: 824

@ExperimentsWithCode

Thank you for your answer again, I have spent almost the whole day today trying to figure out how to do this and I've also tried your suggestion where you add that flag --disable-user-media-security to chrome, unfortunately it didn't work for me.

However I thought of a really simple solution:

To automatically click on Allow all I have to do is press TAB key three times and then press enter. And so I have written the program to do that automatically and it WORKS !!!

The first TAB pressed when my html page opens directs me to my input box, the second to the address bar and the third on the ALLOW button, then the Enter button is pressed.

The python program uses selenium as well as PyWin32 bindings.


Thank you for taking your time and trying to help me it is much appreciated.

Upvotes: 8

ExperimentsWithCode
ExperimentsWithCode

Reputation: 1184

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)

Upvotes: 5

Related Questions