4

I am using Selenium in Python to automate a remote browser. The browser needs access to its webcam and microphone. When I navigate to a page that requests access, Firefox shows a pop-up window that asks "Would you like to share you camera and microphone with [host]?"

This window is not part of the browser's page, so it cannot be detected or controlled via Selenium.

This behavior is controlled by the media.navigator.permission.disabled option in Firefox's 'about:config' page. If this option is set to 'true', then access to the camera should be granted automatically.

When I set that option to 'true', it eliminates the prompt only when I run Firefox manually. When I run Firefox via Selenium, I still get the prompt.

How can I suppress this prompt, and have permission granted automatically?

Martin Del Vecchio
  • 3,558
  • 2
  • 27
  • 36

2 Answers2

1

The problem lies in Firefox profiles. Selenium creates a new, temporary profile for each browser instance. This profile is separate from the profile you use when you manually start Firefox.

Thus, when you set media.navigator.permission.disabled to 'true' in about:config, you do so only for your profile, and not for the profile that Selenium uses.

There are two ways to work around this:

  1. Tell Selenium which existing profile to use.

    To do this, you must first determine which profile you are using. To do this, close all instances of Firefox, then execute firefox -p to start the profile manager. In most cases, you will see a single profile called default.

    Using this profile, navigate to about:config, and set the media.navigator.permission.disabled option to true.

    Then, when you start the Selenium standalone server, specify this profile:

    java -jar selenium-server-standalone-2.37.0.jar -Dwebdriver.firefox.profile=default
    

    This tells Selenium to use the default profile, which has the settings you want.

  2. Create and configure a new profile for Selenium to use.

    Before you create the browser instance, you must create a Firefox profile and configure it to meet your needs:

    profile = webdriver.FirefoxProfile()
    profile.set_preference ('media.navigator.permission.disabled', True)
    profile.update_preferences()
    

    Then specify this profile when you create the remote browser instance:

    firefox = selenium.webdriver.remote.webdriver.WebDriver (command_executor=my_url, desired_capabilities=DesiredCapabilities.FIREFOX, browser_profile=profile)
    

    Selenium will then use this profile, and you should not be prompted for permission to access the camera.

    Note that this method takes more time than the first method.

Remi Guan
  • 21,506
  • 17
  • 64
  • 87
Martin Del Vecchio
  • 3,558
  • 2
  • 27
  • 36
  • 4
    Either you *very* quickly found the answer to your own problem or you are intending this to be a tutorial. – Arran Jan 23 '14 at 14:54
1

You can use options:

from selenium import webdriver
from selenium.webdriver.firefox.options import Options

options = Options()
options.set_preference("media.navigator.permission.disabled", True)
browser = webdriver.Firefox(options=options)
Pegasis
  • 1,256
  • 11
  • 15