According to "Selenium3 Python WebDriver API Source Code Analysis (2): Selenium Package Directory Structure, Module Function Overview", selenium\webdriver\remote\webdriver.py
the WebDriver
classes in the module define the basic characteristics of the general WebDriver, and various browsers inherit the WebDriver
classes according to their own characteristics to make further implementations. selenium\webdriver\firefox\webdriver.py
The WebDriver
class in the module implements FireFox WebDriver and selenium\webdriver\__init__.py
is 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 orremote_connection.RemoteConnection
object. The default is'http://127.0.0.1:4444/wd/hub'
.desired_capabilities
-When creating a new sessioncapabilities
.browser_profile
-selenium.webdriver.firefox.firefox_profile.FirefoxProfile
object. Custom configuration file. Only for Firefox.proxy
-selenium.webdriver.common.proxy.Proxy
object. Proxy settings.keep_alive
- Boolean value. Whether to set a long connection. The default value isFalse
.file_detector
-Custom file processor. The default value isNone
usedLocalFileDetector()
.options
-Options
class instance. Browser option settings.
The attributes are:
session_id
: Session ID.capabilities
: Thecapabilities
collection returned by the browser .command_executor
:remote_connection.RemoteConnection
Object. The execution object of the command.error_handler
:ErrorHandler
Object. 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 capabilities
dictionary, passed to the browser.
For some parameters, such as firefox_profile
and options.profile
are mutually exclusive, their priority is determined by a specific scenario. capabilities
Is the least specific keyword parameter, followed by options
, firefox_binary
and firefox_profile
. Specifically, if both set firefox_profile
and options.profile
parameters for the application at this time firefox_profile
, options.profile
will be ignored. Similarly, capabilities["moz:firefoxOptions"]["profile"]
the priority is lower options.profile
.
The construction method parameters are:
firefox_profile
:FirefoxProfile
Object 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
:FirefoxBinary
Or 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 capabilities
Dictionary, will be obsolete soon.proxy
: Proxy settings.executable_path
:geckodriver
The path, which is obtained from system variablespath
by default .options
:options.Options
Examples.service_log_path
: Log storage path.firefox_options
: Equivalentoptions
, will be obsolete.service_args
: The parameter list passed to the driver service.desired_capabilities
: Same ascapabilities
.log_path
: Sameservice_log_path
and 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 path
is 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.xpi
expanded id
.
Uninstall extension
The method signature is: the def uninstall_addon(self, identifier):
parameter identifier
is the extension returned when the extension is installed id
.