第一句子网 - 唯美句子、句子迷、好句子大全
第一句子网 > Selenium3 Python WebDriver API源码探析(12)FireFox自定义功能(FirefoxOptions)的实现:Options类

Selenium3 Python WebDriver API源码探析(12)FireFox自定义功能(FirefoxOptions)的实现:Options类

时间:2020-05-05 06:57:26

相关推荐

Selenium3 Python WebDriver API源码探析(12)FireFox自定义功能(FirefoxOptions)的实现:Options类

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 profileselenium\webdriver\firefox\firefox_profile.py中的FirefoxProfile类定义了Firefox自定义配置文件(profile)的实现。可用于安装扩展或者自定义证书,或设置首选项设置。FireFox binaryselenium\webdriver\firefox\firefox_binary.pyFirefoxBinary类定义了FireFox二进制文件的管理。

FirefoxOptions实现解析

selenium\webdriver\firefox\options.py中的Options类定义了FirefoxOptions实现。

Options类主要管理以下4种自定义配置功能:

Capabilities:功能 通过属性_caps存储Capabilities。字典结构。默认值为DesiredCapabilities.FIREFOX.copy()。通过capabilities特性返回_caps即全部Capabilities。通过set_capability(name, value)方法设置Capabilitiespreference:首选项设置(在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 = 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

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。