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:firefoxOptions
JSON对象。
class Options(object):KEY = "moz:firefoxOptions"def __init__(self):self._binary = Noneself._preferences = {}self._profile = Noneself._proxy = Noneself._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# 实例化FirefoxOptionsoptions = 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']}}
参考文献
/testing/geckodriver/Capabilities.html#capabilities-example
/en-US/docs/Mozilla/Preferences
/en-US/docs/Mozilla/Preferences/Preference_reference
/en-US/docs/Mozilla/Preferences/A_brief_guide_to_Mozilla_preferences
/en-US/kb/firefox-options-preferences-and-settings