Selenium3 Python WebDriver API source code analysis (18) FireFox WebDriver implementation, installation extension

According to "Selenium3 Python WebDriver API Source Code Analysis (2): Selenium Package Directory Structure, Module Function Overview", selenium\webdriver\remote\webdriver.pythe WebDriverclasses in the module define the basic characteristics of the general WebDriver, and various browsers inherit the WebDriverclasses according to their own characteristics to make further implementations. selenium\webdriver\firefox\webdriver.pyThe WebDriverclass in the module implements FireFox WebDriver and selenium\webdriver\__init__.pyis exposed through the external ( from .firefox.webdriver import WebDriver as Firefox). So generally use the following method to initialize FireFox WebDriver.

from selenium import webdriver
driver = webdriver.Firefox()

remote WebDriver

The class signature of remote WebDriver is: the
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)
constructor parameters are:

  • command_executor: String or remote_connection.RemoteConnectionobject. The default is 'http://127.0.0.1:4444/wd/hub'.
  • desired_capabilities-When creating a new session capabilities.
  • browser_profile- selenium.webdriver.firefox.firefox_profile.FirefoxProfileobject. Custom configuration file. Only for Firefox.
  • proxy- selenium.webdriver.common.proxy.Proxyobject. Proxy settings.
  • keep_alive- Boolean value. Whether to set a long connection. The default value is False.
  • file_detector-Custom file processor. The default value is Noneused LocalFileDetector().
  • options- Optionsclass instance. Browser option settings.

The attributes are:

  • session_id: Session ID.
  • capabilities: The capabilitiescollection returned by the browser .
  • command_executor: remote_connection.RemoteConnectionObject. The execution object of the command.
  • error_handler: ErrorHandlerObject. Error handler.

Firefox WebDriver

The class signature of Firefox WebDriver is:
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 constructor parameters is more complex, there are a number of parameters duplication of functions, there are some parameters about to waste, eventually bringing together these parameters configured as a capabilitiesdictionary, passed to the browser.
For some parameters, such as firefox_profileand options.profileare mutually exclusive, their priority is determined by a specific scenario. capabilitiesIs the least specific keyword parameter, followed by options, firefox_binaryand firefox_profile. Specifically, if both set firefox_profileand options.profileparameters for the application at this time firefox_profile, options.profilewill be ignored. Similarly, capabilities["moz:firefoxOptions"]["profile"]the priority is lower options.profile.

The construction method parameters are:

  • firefox_profile: FirefoxProfileObject or string. If it is not defined, a new custom configuration file will be generated in the temporary directory of the operating system.
  • firefox_binary: FirefoxBinaryOr the path of the Firefox binary file. If not defined, the default installation location of Firefox of the operating system is used.
  • timeout: The timeout period for starting Firefox.
  • capabilities: desired capabilitiesDictionary, will be obsolete soon.
  • proxy: Proxy settings.
  • executable_path: geckodriverThe path, which is obtained from system variables pathby default .
  • options: options.OptionsExamples.
  • service_log_path: Log storage path.
  • firefox_options: Equivalent options, will be obsolete.
  • service_args: The parameter list passed to the driver service.
  • desired_capabilities: Same as capabilities.
  • log_path: Same service_log_pathand will be obsolete.
  • keep_alive: Whether to set a long connection.

The simplified class signature is:
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)

Extension installation and uninstallation methods

The Firefox WebDriver class implements extension installation and uninstallation methods.

Install extension

The method signature is: the def install_addon(self, path, temporary=None):
parameter pathis the absolute path of the extension.
The return value is extended id, easy to use when uninstalling.
Case:

import selenium.webdriver as webdriver

driver = webdriver.Firefox()

addon_id = driver.install_addon(r"\path\d.xpi")
print(addon_id)

The output is: {32af1358-428a-446d-873e-5f8eb5f2a72e}that is, d.xpiexpanded id.
Insert picture description here

Uninstall extension

The method signature is: the def uninstall_addon(self, identifier):
parameter identifieris the extension returned when the extension is installed id.

Guess you like

Origin blog.csdn.net/mighty13/article/details/115113989