FireFox自定义功能的相关实现
根据《Selenium3 Python WebDriver API源码探析(11)WebDriver Capabilities(驱动功能)概述,FirefoxOptions》可知,WebDriver实现通过Capabilities定义新会话启动时的各种特性,也可以返回会话的特性集。Capabilities分为两类,一类是标准Capabilities,一类是各WebDriver实现的特定Capabilities。对于FireFox WebDriver 即geckodriver来说,通过FirefoxOptions来自定义Capabilities。
selenium\webdriver\firefox\options.py中的Options类定义了FirefoxOptions实现。其他辅助实现如下:
DesiredCapabilities:为了兼容,Options类保留了即将废弃的DesiredCapabilities实现,selenium\webdriver\common\desired_capabilities.py中的DesiredCapabilities定义了各种实现的公共Capabilities。通过DesiredCapabilities.FIREFOX.copy()可复制Capabilities数据结构用于自定义。FireFox profile:selenium\webdriver\firefox\firefox_profile.py中的FirefoxProfile类定义了Firefox自定义配置文件(profile)的实现。可用于安装扩展或者自定义证书,或设置首选项设置。FireFox binary:selenium\webdriver\firefox\firefox_binary.py中FirefoxBinary类定义了FireFox二进制文件的管理。
FirefoxOptions实现解析
selenium\webdriver\firefox\options.py中的Options类定义了FirefoxOptions实现。
Options类主要管理以下4种自定义配置功能:
Capabilities:功能- 通过属性
_caps存储Capabilities。字典结构。默认值为DesiredCapabilities.FIREFOX.copy()。 - 通过
capabilities特性返回_caps即全部Capabilities。 - 通过
set_capability(name, value)方法设置Capabilities。
- 通过属性
preference:首选项设置(在firefox地址栏中输入about:config即可查看自定义首选项)。- 通过属性
_preferences存储首选项设置。字典结构。默认值为{}。 - 通过
preferences特性返回_preferences。 - 通过
set_preference(name, value)方法设置首选项设置。
- 通过属性
arguments:Firefox进程参数- 通过属性
_arguments存储Firefox进程参数。列表结构。默认值为[]。 - 通过
arguments特性返回_arguments。 - 通过
add_argument( argument)方法设置Firefox进程参数。
- 通过属性
profile:自定义配置文件- 通过属性
_profile存储自定义配置文件位置。字符串或FirefoxProfile对象。默认值为None。 - 通过
profile特性返回_profile。 - 通过
profile(new_profile)方法设置自定义配置文件位置。
- 通过属性
Options类还提供了以下快捷功能:
binary:设置Firefox二进制文件- 通过属性
_binary存储设置Firefox二进制文件位置。字符串或FirefoxBinary对象。默认值为None。 - 通过
binary特性返回_binary。 - 通过
binary(new_binary)方法设置Firefox二进制文件位置。
- 通过属性
acceptInsecureCerts:设置是否接受不安全的认证。本质是通过Capabilities设置(_caps['acceptInsecureCerts'])。- 通过
accept_insecure_certs特性获取_caps['acceptInsecureCerts']值。 - 通过
accept_insecure_certs( value)方法设置_caps['acceptInsecureCerts']值。
- 通过
headless:设置无头模式。本质设置arguments中的'-headless'。需要注意的是'--headless'也可设置成功,但是下面的特性可能不能检测到。- 通过
headless特性获取arguments中是否包含'-headless'。 - 通过
headless( value)方法向arguments中添加'-headless'。。
- 通过
to_capabilities:将Options实例转换为标准的moz:firefoxOptionsJSON对象。
class Options(object):
KEY = "moz:firefoxOptions"
def __init__(self):
self._binary = None
self._preferences = {}
self._profile = None
self._proxy = None
self._caps = DesiredCapabilities.FIREFOX.copy()
self._arguments = []
self.log = Log()
Options类的应用
通过以下案例可知:
只用Options类便可以实现自定义功能。capabilities属性返回了完整的功能结构。
由于from .firefox.options import Options as FirefoxOptions,因此可使用webdriver.FirefoxOptions()实例化Options类。
通过webdriver.Firefox(options)加载自定义功能。
源码:
from selenium import webdriver
# 实例化FirefoxOptions
options = webdriver.FirefoxOptions()
# 设置webdriver进程参数
options.add_argument("-headless")
options.add_argument("--disable-gpu")
# 设置首选项设置
options.set_preference("dom.webdriver.enabled", False)
# 加载自定义功能
driver = webdriver.Firefox(options=options)
# 输出webdriver进程参数
print(options.arguments)
# 输出无头浏览状态
print(options.headless)
# 设置首选项设置
js="return window.navigator.webdriver"
result=driver.execute_script(js)
# 输出首选项设置
print(options.preferences)
# 输出webdriver的功能集
print(options.capabilities)
driver.quit()
输出为:
['-headless', '--disable-gpu']
True
{'dom.webdriver.enabled': False}
{'browserName': 'firefox', 'marionette': True, 'acceptInsecureCerts': True, 'moz:firefoxOptions': {'prefs': {'dom.webdriver.enabled': False}, 'args': ['-headless', '--disable-gpu']}}
参考文献
https://docs.mozilla.org/testing/geckodriver/Capabilities.html#capabilities-example
https://developer.mozilla.org/en-US/docs/Mozilla/Preferences
https://developer.mozilla.org/en-US/docs/Mozilla/Preferences/Preference_reference
https://developer.mozilla.org/en-US/docs/Mozilla/Preferences/A_brief_guide_to_Mozilla_preferences
https://support.mozilla.org/en-US/kb/firefox-options-preferences-and-settings
本文介绍 Selenium 中 Firefox 的自定义配置方法,包括通过 FirefoxOptions 设置功能、首选项、进程参数及自定义配置文件等内容。

被折叠的 条评论
为什么被折叠?



