使用Selenium/WebDriver和Python,如何抑制共享相机和麦克风的提示? [英] Using Selenium/WebDriver and Python, how do I suppress the prompt to share my camera and microphone?

查看:338
本文介绍了使用Selenium/WebDriver和Python,如何抑制共享相机和麦克风的提示?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Python中使用Selenium来自动化远程浏览器.浏览器需要访问其网络摄像头和麦克风.当我导航到一个请求访问的页面时,Firefox显示一个弹出窗口,询问您是否想与[主机]共享相机和麦克风?"

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]?"

此窗口不是浏览器页面的一部分,因此无法通过Selenium进行检测或控制.

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

此行为由Firefox的"about:config"页面中的media.navigator.permission.disabled选项控制.如果此选项设置为"true",则应自动授予对摄像机的访问权限.

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.

当我将该选项设置为"true"时,仅当我手动运行Firefox时,它才会消除提示.通过Selenium运行Firefox时,仍然出现提示.

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?

推荐答案

问题出在Firefox配置文件中. Selenium为每个浏览器实例创建一个新的临时配置文件.该配置文件与您手动启动Firefox时使用的配置文件不同.

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.

因此,当在about:config中将media.navigator.permission.disabled设置为'true'时,仅对您的配置文件执行此操作,而不对Selenium使用的配置文件进行操作.

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. 告诉Selenium要使用的现有配置文件.

  1. Tell Selenium which existing profile to use.

为此,您必须首先确定所使用的配置文件.为此,请关闭Firefox的所有实例,然后执行firefox -p以启动配置文件管理器.在大多数情况下,您会看到一个名为default的配置文件.

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.

使用此配置文件,导航到about:config,然后将media.navigator.permission.disabled选项设置为true.

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

然后,当您启动Selenium独立服务器时,请指定此配置文件:

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

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

这告诉Selenium使用default配置文件,该配置文件具有您想要的设置.

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

创建并配置供Selenium使用的新配置文件.

Create and configure a new profile for Selenium to use.

在创建浏览器实例之前,必须创建一个Firefox配置文件并对其进行配置以满足您的需求:

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将使用此配置文件,并且不会提示您访问相机的权限.

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.

这篇关于使用Selenium/WebDriver和Python,如何抑制共享相机和麦克风的提示?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆