Selenium3 Python WebDriver API源码探析(18)FireFox WebDriver实现,安装扩展

根据《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_executorremote_connection.RemoteConnection 对象。命令的执行对象。
  • error_handlerErrorHandler 对象。错误处理器。

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是最不具体的关键字参数,其次是optionsfirefox_binaryfirefox_profile。具体来讲,如果同时设置 firefox_profileoptions.profile,此时应用的参数为firefox_profileoptions.profile将被忽略。类似地capabilities["moz:firefoxOptions"]["profile"] 的优先级低于options.profile

构造方法参数为:

  • firefox_profileFirefoxProfile 对象或字符串。如果没有定义,将在操作系统的临时目录中生成一个新的自定义配置文件。
  • firefox_binaryFirefoxBinary 或Firefox二进制文件的路径。如果没有定义,则使用操作系统的Firefox的默认安装位置。
  • timeout: 启动Firefox的超时时间。
  • capabilitiesdesired capabilities字典,即将废弃。
  • proxy:代理设置。
  • executable_pathgeckodriver的路径,默认从系统变量path中获取。
  • optionsoptions.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

猜你喜欢

转载自blog.csdn.net/mighty13/article/details/115113989
今日推荐