Python bindings for Selenium WebDriver providing automated browser control for multiple browsers including Chrome, Firefox, Edge, Safari, and Internet Explorer
—
This document covers the WebDriver classes for different browsers in Python Selenium WebDriver. Each browser-specific WebDriver inherits from the remote WebDriver and provides browser-specific functionality.
{ .api }
from selenium.webdriver.remote.webdriver import WebDriver
class WebDriver:
def __init__(
self,
command_executor: Union[str, RemoteConnection] = 'http://127.0.0.1:4444/wd/hub',
desired_capabilities: Optional[dict] = None,
browser_profile: Optional[BaseOptions] = None,
proxy: Optional[Proxy] = None,
keep_alive: bool = False,
file_detector: Optional[FileDetector] = None,
options: Optional[BaseOptions] = None,
service: Optional[Service] = None,
strict_file_interactability: bool = True
) -> NoneDescription: The base WebDriver class for remote browser automation.
Parameters:
command_executor: URL of the WebDriver server or RemoteConnection instancedesired_capabilities: Dictionary of browser capabilities (deprecated)browser_profile: Browser profile (deprecated, use options instead)proxy: Proxy configurationkeep_alive: Whether to keep HTTP connection alivefile_detector: File detector for handling file uploadsoptions: Browser options objectservice: Service object for local driversstrict_file_interactability: Whether to enforce strict file interactionExample:
from selenium import webdriver
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
# Remote WebDriver
driver = webdriver.Remote(
command_executor='http://localhost:4444/wd/hub',
options=webdriver.ChromeOptions()
){ .api }
from selenium.webdriver.chrome.webdriver import WebDriver as Chrome
class WebDriver(ChromiumDriver):
def __init__(
self,
options: Optional[ChromeOptions] = None,
service: Optional[ChromeService] = None,
keep_alive: bool = True,
) -> NoneDescription: Controls the ChromeDriver and allows you to drive the Chrome browser.
Parameters:
options: Instance of ChromeOptions for browser configurationservice: Service object for handling the ChromeDriver processkeep_alive: Whether to configure ChromeRemoteConnection to use HTTP keep-aliveExample:
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.chrome.service import Service
options = Options()
options.add_argument('--headless')
service = Service('/path/to/chromedriver')
driver = webdriver.Chrome(options=options, service=service){ .api }
from selenium.webdriver.firefox.webdriver import WebDriver as Firefox
class WebDriver(RemoteWebDriver):
CONTEXT_CHROME = "chrome"
CONTEXT_CONTENT = "content"
def __init__(
self,
options: Optional[FirefoxOptions] = None,
service: Optional[FirefoxService] = None,
keep_alive: bool = True,
) -> NoneDescription: Controls the GeckoDriver and allows you to drive the Firefox browser.
Parameters:
options: Instance of FirefoxOptions for browser configurationservice: Service instance for managing the GeckoDriver processkeep_alive: Whether to configure RemoteConnection to use HTTP keep-aliveFirefox-Specific Methods:
{ .api }
def set_context(self, context: str) -> NoneDescription: Sets the context that Selenium commands are running in.
Parameters:
context: Either CONTEXT_CHROME or CONTEXT_CONTENT{ .api }
@contextmanager
def context(self, context: str)Description: Context manager for temporarily switching context.
Parameters:
context: Either CONTEXT_CHROME or CONTEXT_CONTENT{ .api }
def install_addon(self, path: str, temporary: bool = False) -> strDescription: Installs Firefox addon.
Parameters:
path: Absolute path to the addon that will be installedtemporary: Whether to load the extension temporarily during the sessionReturns: Identifier of installed addon
{ .api }
def uninstall_addon(self, identifier: str) -> NoneDescription: Uninstalls Firefox addon using its identifier.
Parameters:
identifier: The addon identifier returned by install_addon(){ .api }
def get_full_page_screenshot_as_file(self, filename: str) -> boolDescription: Saves a full document screenshot to a PNG file.
Parameters:
filename: The full path where to save the screenshot (should end with .png)Returns: True if successful, False otherwise
{ .api }
def get_full_page_screenshot_as_png(self) -> bytesDescription: Gets the full document screenshot as binary data.
Returns: PNG screenshot data as bytes
{ .api }
def get_full_page_screenshot_as_base64(self) -> strDescription: Gets the full document screenshot as a base64 encoded string.
Returns: Base64 encoded PNG screenshot
Example:
from selenium import webdriver
from selenium.webdriver.firefox.options import Options
options = Options()
options.headless = True
driver = webdriver.Firefox(options=options)
# Firefox-specific features
with driver.context(driver.CONTEXT_CHROME):
# Chrome context operations
pass
addon_id = driver.install_addon('/path/to/addon.xpi')
driver.uninstall_addon(addon_id){ .api }
from selenium.webdriver.edge.webdriver import WebDriver as Edge
class WebDriver(ChromiumDriver):
def __init__(
self,
options: Optional[EdgeOptions] = None,
service: Optional[EdgeService] = None,
keep_alive: bool = True,
) -> NoneDescription: Controls the EdgeDriver and allows you to drive the Microsoft Edge browser.
Parameters:
options: Instance of EdgeOptions for browser configurationservice: Service object for handling the EdgeDriver processkeep_alive: Whether to configure EdgeRemoteConnection to use HTTP keep-aliveExample:
from selenium import webdriver
from selenium.webdriver.edge.options import Options
options = Options()
options.add_argument('--disable-extensions')
driver = webdriver.Edge(options=options){ .api }
from selenium.webdriver.safari.webdriver import WebDriver as Safari
class WebDriver(RemoteWebDriver):
def __init__(
self,
options: Optional[SafariOptions] = None,
service: Optional[SafariService] = None,
keep_alive: bool = True,
) -> NoneDescription: Controls the SafariDriver and allows you to drive the Safari browser.
Parameters:
options: Instance of SafariOptions for browser configurationservice: Service instance for managing the SafariDriver processkeep_alive: Whether to configure SafariRemoteConnection to use HTTP keep-aliveExample:
from selenium import webdriver
driver = webdriver.Safari(){ .api }
from selenium.webdriver.ie.webdriver import WebDriver as InternetExplorer
class WebDriver(RemoteWebDriver):
def __init__(
self,
options: Optional[IeOptions] = None,
service: Optional[IeService] = None,
keep_alive: bool = True,
) -> NoneDescription: Controls the IEDriver and allows you to drive Internet Explorer.
Parameters:
options: Instance of IeOptions for browser configurationservice: Service instance for managing the IEDriver processkeep_alive: Whether to configure IeRemoteConnection to use HTTP keep-aliveExample:
from selenium import webdriver
from selenium.webdriver.ie.options import Options
options = Options()
options.ignore_protected_mode_settings = True
driver = webdriver.Ie(options=options){ .api }
from selenium.webdriver.webkitgtk.webdriver import WebDriver as WebKitGTK
class WebDriver(RemoteWebDriver):
def __init__(
self,
options: Optional[WebKitGTKOptions] = None,
service: Optional[WebKitGTKService] = None,
keep_alive: bool = True,
) -> NoneDescription: Controls the WebKitGTKDriver for GTK-based WebKit browsers.
Parameters:
options: Instance of WebKitGTKOptions for browser configurationservice: Service instance for managing the WebKitGTKDriver processkeep_alive: Whether to configure RemoteConnection to use HTTP keep-alive{ .api }
from selenium.webdriver.wpewebkit.webdriver import WebDriver as WPEWebKit
class WebDriver(RemoteWebDriver):
def __init__(
self,
options: Optional[WPEWebKitOptions] = None,
service: Optional[WPEWebKitService] = None,
keep_alive: bool = True,
) -> NoneDescription: Controls the WPEWebKitDriver for WPE WebKit browsers.
Parameters:
options: Instance of WPEWebKitOptions for browser configurationservice: Service instance for managing the WPEWebKitDriver processkeep_alive: Whether to configure RemoteConnection to use HTTP keep-aliveAll WebDriver classes inherit common methods from the base RemoteWebDriver:
{ .api }
def get(self, url: str) -> NoneDescription: Loads a web page in the current browser session.
Parameters:
url: URL to load{ .api }
def quit(self) -> NoneDescription: Quits the driver and closes every associated window.
{ .api }
def close(self) -> NoneDescription: Closes the current window.
{ .api }
def refresh(self) -> NoneDescription: Refreshes the current page.
{ .api }
def back(self) -> NoneDescription: Goes one step backward in the browser history.
{ .api }
def forward(self) -> NoneDescription: Goes one step forward in the browser history.
Example:
# Universal pattern for any browser
from selenium import webdriver
# Choose your browser
driver = webdriver.Chrome() # or Firefox(), Edge(), Safari(), etc.
try:
driver.get('https://example.com')
driver.refresh()
driver.back()
driver.forward()
finally:
driver.quit()Example of browser selection:
import os
from selenium import webdriver
def get_driver(browser_name: str):
"""Factory function to get the appropriate WebDriver"""
browser_name = browser_name.lower()
if browser_name == 'chrome':
return webdriver.Chrome()
elif browser_name == 'firefox':
return webdriver.Firefox()
elif browser_name == 'edge':
return webdriver.Edge()
elif browser_name == 'safari':
return webdriver.Safari()
else:
raise ValueError(f"Unsupported browser: {browser_name}")
# Use environment variable or config
browser = os.getenv('BROWSER', 'chrome')
driver = get_driver(browser)Install with Tessl CLI
npx tessl i tessl/pypi-selenium