0
# Selenium WebDriver
1
2
Selenium WebDriver is the official Python bindings for Selenium, providing automated browser control and testing capabilities. It supports multiple browsers including Chrome, Firefox, Edge, Safari, and Internet Explorer, enabling cross-browser web application testing and automation.
3
4
## Package Information
5
6
- **Package Name**: selenium
7
- **Package Type**: PyPI
8
- **Language**: Python
9
- **Installation**: `pip install selenium`
10
11
## Core Imports
12
13
```python
14
from selenium import webdriver
15
from selenium.webdriver.common.by import By
16
from selenium.webdriver.common.keys import Keys
17
from selenium.webdriver.common.action_chains import ActionChains
18
```
19
20
For browser-specific configuration:
21
22
```python
23
from selenium.webdriver.chrome.service import Service as ChromeService
24
from selenium.webdriver.chrome.options import Options as ChromeOptions
25
from selenium.webdriver.firefox.service import Service as FirefoxService
26
from selenium.webdriver.firefox.options import Options as FirefoxOptions
27
```
28
29
## Basic Usage
30
31
```python
32
from selenium import webdriver
33
from selenium.webdriver.common.by import By
34
from selenium.webdriver.support.wait import WebDriverWait
35
from selenium.webdriver.support import expected_conditions as EC
36
37
# Create WebDriver instance
38
driver = webdriver.Chrome()
39
40
# Navigate to a page
41
driver.get("https://example.com")
42
43
# Find elements and interact
44
element = driver.find_element(By.ID, "search-box")
45
element.send_keys("selenium python")
46
element.submit()
47
48
# Use explicit waits
49
wait = WebDriverWait(driver, 10)
50
results = wait.until(EC.presence_of_element_located((By.CLASS_NAME, "results")))
51
52
# Take screenshot
53
driver.save_screenshot("results.png")
54
55
# Clean up
56
driver.quit()
57
```
58
59
## Architecture
60
61
Selenium WebDriver follows a modular architecture:
62
63
- **WebDriver Classes**: Browser-specific drivers (Chrome, Firefox, Edge, Safari, IE, Remote)
64
- **Service Objects**: Manage browser driver processes and configuration
65
- **Options Objects**: Configure browser behavior, extensions, and capabilities
66
- **Support Modules**: Utilities for waits, element selection, actions, and expected conditions
67
- **WebElement Interface**: Represents and interacts with DOM elements
68
- **Exception Hierarchy**: Comprehensive error handling for various failure scenarios
69
70
## Capabilities
71
72
### WebDriver Classes and Browser Control
73
74
Core WebDriver classes for controlling different browsers with navigation, window management, and page interaction capabilities.
75
76
```python { .api }
77
class Chrome(ChromiumDriver):
78
def __init__(self, options: Options = None, service: Service = None, keep_alive: bool = True) -> None: ...
79
80
class Firefox(WebDriver):
81
def __init__(self, options: Options = None, service: Service = None, keep_alive: bool = True) -> None: ...
82
83
class Edge(ChromiumDriver):
84
def __init__(self, options: Options = None, service: Service = None, keep_alive: bool = True) -> None: ...
85
86
class Safari(WebDriver):
87
def __init__(self, options: Options = None, service: Service = None, keep_alive: bool = True) -> None: ...
88
89
class Remote(WebDriver):
90
def __init__(self, command_executor: str = 'http://127.0.0.1:4444', desired_capabilities: dict = None) -> None: ...
91
```
92
93
[WebDriver Classes](./webdriver-classes.md)
94
95
### Element Location and Interaction
96
97
Methods for finding elements on the page and interacting with them through clicks, text input, and attribute access.
98
99
```python { .api }
100
class WebDriver:
101
def find_element(self, by: str, value: str) -> WebElement: ...
102
def find_elements(self, by: str, value: str) -> list[WebElement]: ...
103
def get(self, url: str) -> None: ...
104
def current_url(self) -> str: ...
105
def title(self) -> str: ...
106
def page_source(self) -> str: ...
107
def close(self) -> None: ...
108
def quit(self) -> None: ...
109
110
class WebElement:
111
def click(self) -> None: ...
112
def send_keys(self, *value: str) -> None: ...
113
def clear(self) -> None: ...
114
def get_attribute(self, name: str) -> str | None: ...
115
def is_displayed(self) -> bool: ...
116
def is_enabled(self) -> bool: ...
117
def is_selected(self) -> bool: ...
118
```
119
120
[Element Location and Interaction](./element-interaction.md)
121
122
### Action Chains and Complex Interactions
123
124
Advanced user interaction simulation including mouse movements, keyboard combinations, drag and drop operations.
125
126
```python { .api }
127
class ActionChains:
128
def __init__(self, driver: WebDriver, duration: int = 250) -> None: ...
129
def click(self, on_element: WebElement = None) -> ActionChains: ...
130
def click_and_hold(self, on_element: WebElement = None) -> ActionChains: ...
131
def context_click(self, on_element: WebElement = None) -> ActionChains: ...
132
def double_click(self, on_element: WebElement = None) -> ActionChains: ...
133
def drag_and_drop(self, source: WebElement, target: WebElement) -> ActionChains: ...
134
def move_to_element(self, to_element: WebElement) -> ActionChains: ...
135
def send_keys(self, *keys_to_send: str) -> ActionChains: ...
136
def perform(self) -> None: ...
137
```
138
139
[Action Chains](./action-chains.md)
140
141
### Waits and Expected Conditions
142
143
Explicit and implicit wait mechanisms with pre-built conditions for synchronizing test execution with page state.
144
145
```python { .api }
146
class WebDriverWait:
147
def __init__(self, driver: WebDriver, timeout: float, poll_frequency: float = 0.5) -> None: ...
148
def until(self, method: Callable, message: str = "") -> Any: ...
149
def until_not(self, method: Callable, message: str = "") -> Any: ...
150
151
# Expected Conditions module functions
152
def title_is(title: str) -> Callable: ...
153
def presence_of_element_located(locator: tuple[str, str]) -> Callable: ...
154
def visibility_of_element_located(locator: tuple[str, str]) -> Callable: ...
155
def element_to_be_clickable(locator: tuple[str, str]) -> Callable: ...
156
def text_to_be_present_in_element(locator: tuple[str, str], text: str) -> Callable: ...
157
```
158
159
[Waits and Expected Conditions](./waits-conditions.md)
160
161
### Browser Configuration and Options
162
163
Configuration options for customizing browser behavior, setting up proxies, and managing browser capabilities.
164
165
```python { .api }
166
class Options:
167
def add_argument(self, argument: str) -> None: ...
168
def add_extension(self, extension: str) -> None: ...
169
def add_experimental_option(self, name: str, value: Any) -> None: ...
170
def set_capability(self, name: str, value: Any) -> None: ...
171
172
class Service:
173
def __init__(self, executable_path: str = None, port: int = 0, service_args: list = None) -> None: ...
174
def start(self) -> None: ...
175
def stop(self) -> None: ...
176
177
class Proxy:
178
def __init__(self) -> None: ...
179
http_proxy: str
180
ssl_proxy: str
181
proxy_type: ProxyType
182
```
183
184
[Browser Configuration](./browser-configuration.md)