Martin Del Vecchio
Martin Del Vecchio

Reputation: 3848

Using Selenium/WebDriver and Python, how do I suppress the prompt to share my camera and microphone?

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?

Upvotes: 5

Views: 7353

Answers (2)

Pegasis
Pegasis

Reputation: 1424

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)

Upvotes: 1

Martin Del Vecchio
Martin Del Vecchio

Reputation: 3848

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.

Upvotes: 2

Related Questions