Accept permission request in chrome using selenium
Asked Answered
A

9

9

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 ?

Ashtoreth answered 7/2, 2014 at 13:23 Comment(8)
Are you able to use firefox? My experience with these dialogs in firefox is that you have to set your profile to allow it by default. As of 1 year ago, you could not use a custom chrome profile and I haven't seen anything to the contrary. Also, I tried looking into setting a chrome profile to allow mic use by default, and that menu doesn't work for me. Its seems like a known errorChaille
Unfortunatley i HAVE to use chrome because googles web speech is only for chromeAshtoreth
Can you check to see if you can add exceptions for the Mic to your profile. Its not working on my machine, but maybe it will work on yours. 1. Open a new tab 2. In the Chrome menu, click preferences 3. Click Show Advanced Settings (its in the settings tab if you don't open to that page) 4. Click Content Settings under Privacy 5. Scroll down to Media and click Manage Exceptions 6. From here you are supposed to be able to add sites and set them to allow microphoneChaille
Thank you for your answer but this doesn't work for meAshtoreth
Sorry I wasn't more helpful. Good luck.Chaille
@stackoverflow.com/users/6091876/the-arcadian-pandit asks: Shady, I don't have enough reputation to comment. So I have typed as an answer itself. Can you please post the code for simulating the 3 tabs + 1 enter click. I cant find any workaround. I am doing the exact same thing as you. Google speech API testing with CLI python Please help me out. Thanks in advanced :)Irrespirable
Here's a snippet: pastebin.com/9EHQBeKV Nothing spectacular in that piece of code by itselfAshtoreth
Keep in mind that although I've got my project to work with this whole tab key hack, it's fragile. Any unexpected interrupt in that tab sequence and the code would go pear shaped. Nowadays if I wanted to re-do the project I'd have to investigate a better way, and, drop this whole selenium automation altogether.Ashtoreth
A
8

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

Ashtoreth answered 10/2, 2014 at 19:48 Comment(7)
If you could give me your e-mail I'd send you a video of itAshtoreth
It's cool. I should have thought of that. I've read about that before but I use a Mac so PyWin32 doesn't work for me, and I haven't found a suitable replacement. Thanks for the offer though!Chaille
This key sequence doesn't seem to be working for me. Is it still working for you?Bowse
It's been 2 years since I've seen that project. What's not working for you with the key sequence ? does the TAB key miss the allow button ? does it not work at all ? specify more precisely what's happening please.Ashtoreth
Hi there, I know it has been long time ago, but just happened to this when I tried to find an workaround for the exact same problem. I think toggling with keyboard is doable and gave it a shot, however, for the Chrome on my Mac and Linux machine, the "allow" and "deny" on the popup just can't be selected/focused. However, trying on the Chrome on Windows machine did work. Just wondering, have you tried on Chrome Max/Linux before? Or happen to know the tricks there? One site I found with popup you can easily try is digitaltrends.com/computing/best-chrome-themes. Thanks!Endowment
Wouldn't know a thing about doing it on Mac/Linux unfortunatelyAshtoreth
@Endowment See my answer. It also didnt work for me in Linux, but I found a work around using xdotoolErund
H
16

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)
Homeward answered 26/2, 2016 at 15:2 Comment(0)
A
8

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

Ashtoreth answered 10/2, 2014 at 19:48 Comment(7)
If you could give me your e-mail I'd send you a video of itAshtoreth
It's cool. I should have thought of that. I've read about that before but I use a Mac so PyWin32 doesn't work for me, and I haven't found a suitable replacement. Thanks for the offer though!Chaille
This key sequence doesn't seem to be working for me. Is it still working for you?Bowse
It's been 2 years since I've seen that project. What's not working for you with the key sequence ? does the TAB key miss the allow button ? does it not work at all ? specify more precisely what's happening please.Ashtoreth
Hi there, I know it has been long time ago, but just happened to this when I tried to find an workaround for the exact same problem. I think toggling with keyboard is doable and gave it a shot, however, for the Chrome on my Mac and Linux machine, the "allow" and "deny" on the popup just can't be selected/focused. However, trying on the Chrome on Windows machine did work. Just wondering, have you tried on Chrome Max/Linux before? Or happen to know the tricks there? One site I found with popup you can easily try is digitaltrends.com/computing/best-chrome-themes. Thanks!Endowment
Wouldn't know a thing about doing it on Mac/Linux unfortunatelyAshtoreth
@Endowment See my answer. It also didnt work for me in Linux, but I found a work around using xdotoolErund
C
5

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)
Chaille answered 10/2, 2014 at 16:35 Comment(0)
F
1

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());
    }
Formulary answered 7/3, 2017 at 3:59 Comment(0)
E
1

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')
Erund answered 28/10, 2018 at 23:28 Comment(0)
O
0

[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);
    }
 }
Oma answered 4/2, 2016 at 14:3 Comment(1)
D
0

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)
Dworman answered 22/12, 2016 at 19:37 Comment(0)
C
0

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

Culbert answered 19/10, 2020 at 14:2 Comment(0)
V
0

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!

Vorfeld answered 12/10, 2023 at 0:39 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.