根据《Selenium3 Python WebDriver API源码探析(2):Selenium包目录结构、模块功能概述》可知,selenium\webdriver\remote\webdriver.py
模块中的WebDriver
类定义了通用WebDriver的基本特性,各种浏览器根据自身特性继承WebDriver
类再做进一步实现。selenium\webdriver\firefox\webdriver.py
模块中的WebDriver
类实现了FireFox WebDriver,并通过selenium\webdriver\__init__.py
向外暴露(from .firefox.webdriver import WebDriver as Firefox
)。所以一般使用如下方式初始化FireFox WebDriver。
from selenium import webdriver
driver = webdriver.Firefox()
remote WebDriver
remote WebDriver的类签名为:
Class WebDriver(command_executor='http://127.0.0.1:4444/wd/hub', desired_capabilities=None, browser_profile=None, proxy=None, keep_alive=False, file_detector=None, options=None)
构造方法参数为:
command_executor
:字符串或remote_connection.RemoteConnection
对象。默认为'http://127.0.0.1:4444/wd/hub'
。desired_capabilities
- 新建会话时的capabilities
。browser_profile
-selenium.webdriver.firefox.firefox_profile.FirefoxProfile
对象。自定义配置文件。只用于Firefox。proxy
-selenium.webdriver.common.proxy.Proxy
对象。代理设置。keep_alive
- 布尔值。是否设置长连接。默认值为False
。file_detector
- 自定义文件处理器。默认值为None
,使用LocalFileDetector()
。options
-Options
类实例。浏览器选项设置。
属性为:
session_id
:会话ID。capabilities
:浏览器返回的capabilities
集合。command_executor
:remote_connection.RemoteConnection
对象。命令的执行对象。error_handler
:ErrorHandler
对象。错误处理器。
Firefox WebDriver
Firefox WebDriver的类签名为:
Class WebDriver(firefox_profile=None, firefox_binary=None, timeout=30, capabilities=None, proxy=None, executable_path="geckodriver", options=None, service_log_path="geckodriver.log", firefox_options=None, service_args=None, desired_capabilities=None, log_path=None, keep_alive=True)
Firefox WebDriver构造函数的参数比较复杂,有多个参数功能重复,有一些参数即将废弃,最终这些参数汇集构造成为 capabilities
字典,传递给浏览器。
对于某些参数,如 firefox_profile
andoptions.profile
是互斥的,他们的优先级由特定场景决定。capabilities
是最不具体的关键字参数,其次是options
、firefox_binary
和firefox_profile
。具体来讲,如果同时设置 firefox_profile
和options.profile
,此时应用的参数为firefox_profile
,options.profile
将被忽略。类似地capabilities["moz:firefoxOptions"]["profile"]
的优先级低于options.profile
。
构造方法参数为:
firefox_profile
:FirefoxProfile
对象或字符串。如果没有定义,将在操作系统的临时目录中生成一个新的自定义配置文件。firefox_binary
:FirefoxBinary
或Firefox二进制文件的路径。如果没有定义,则使用操作系统的Firefox的默认安装位置。timeout
: 启动Firefox的超时时间。capabilities
:desired capabilities
字典,即将废弃。proxy
:代理设置。executable_path
:geckodriver
的路径,默认从系统变量path
中获取。options
:options.Options
实例。service_log_path
: 日志存放路径。firefox_options
:等同options
,即将废弃。service_args
:传递给driver服务的参数列表。desired_capabilities
:等同于capabilities
。log_path
: 等同service_log_path
,即将废弃。keep_alive
:是否设置长连接。
精简后的类签名为:
Class WebDriver(firefox_profile=None, firefox_binary=None, timeout=30, executable_path="geckodriver", options=None, service_log_path="geckodriver.log", service_args=None, desired_capabilities=None, keep_alive=True)
扩展安装、卸载方法
Firefox WebDriver类实现了扩展安装、卸载方法。
安装扩展
方法签名为:def install_addon(self, path, temporary=None):
参数path
为扩展的绝对路径。
返回值为扩展的id
,便于卸载时使用。
案例:
import selenium.webdriver as webdriver
driver = webdriver.Firefox()
addon_id = driver.install_addon(r"\path\d.xpi")
print(addon_id)
输出为:{32af1358-428a-446d-873e-5f8eb5f2a72e}
即d.xpi
扩展的id
。
卸载扩展
方法签名为:def uninstall_addon(self, identifier):
参数identifier
即安装扩展时返回的扩展id
。