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 ?

Shady Programmer
  • 774
  • 3
  • 9
  • 21
  • 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](http://stackoverflow.com/questions/13840855/selenium-webdriver-python-opening-chrome-browser-to-run-test-case-does-not-load) 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 error](https://code.google.com/p/chromium/issues/detail?id=248987) – ExperimentsWithCode Feb 07 '14 at 17:41
  • Unfortunatley i HAVE to use chrome because googles web speech is only for chrome – Shady Programmer Feb 07 '14 at 19:30
  • 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 microphone – ExperimentsWithCode Feb 07 '14 at 21:57
  • Thank you for your answer but this doesn't work for me – Shady Programmer Feb 10 '14 at 13:37
  • Sorry I wasn't more helpful. Good luck. – ExperimentsWithCode Feb 10 '14 at 16:17
  • @http://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 :) – gia Oct 09 '16 at 08:37
  • Here's a snippet: http://pastebin.com/9EHQBeKV Nothing spectacular in that piece of code by itself – Shady Programmer Oct 10 '16 at 19:30
  • 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. – Shady Programmer Oct 10 '16 at 19:36

8 Answers8

15

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)
sokkyoku
  • 2,071
  • 1
  • 19
  • 22
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.

Shady Programmer
  • 774
  • 3
  • 9
  • 21
  • If you could give me your e-mail I'd send you a video of it – Shady Programmer Feb 14 '14 at 18:15
  • 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! – ExperimentsWithCode Feb 17 '14 at 14:55
  • 1
    This key sequence doesn't seem to be working for me. Is it still working for you? – Connor Feb 24 '16 at 23:24
  • 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. – Shady Programmer Feb 24 '16 at 23:55
  • 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 https://www.digitaltrends.com/computing/best-chrome-themes/. Thanks! – Mengyuan Jul 20 '18 at 14:56
  • Wouldn't know a thing about doing it on Mac/Linux unfortunately – Shady Programmer Jul 22 '18 at 09:50
  • @Mengyuan See my answer. It also didnt work for me in Linux, but I found a work around using **xdotool** – sebpiq Oct 28 '18 at 23:30
4

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)
Community
  • 1
  • 1
ExperimentsWithCode
  • 1,164
  • 4
  • 13
  • 30
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());
    }
Mykola
  • 312
  • 1
  • 7
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')
sebpiq
  • 7,212
  • 8
  • 47
  • 68
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);
    }
 }
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)
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

Ashot Agabekov
  • 173
  • 1
  • 9