0
# WebDriver Classes
1
2
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.
3
4
## Base WebDriver Class
5
6
{ .api }
7
```python
8
from selenium.webdriver.remote.webdriver import WebDriver
9
10
class WebDriver:
11
def __init__(
12
self,
13
command_executor: Union[str, RemoteConnection] = 'http://127.0.0.1:4444/wd/hub',
14
desired_capabilities: Optional[dict] = None,
15
browser_profile: Optional[BaseOptions] = None,
16
proxy: Optional[Proxy] = None,
17
keep_alive: bool = False,
18
file_detector: Optional[FileDetector] = None,
19
options: Optional[BaseOptions] = None,
20
service: Optional[Service] = None,
21
strict_file_interactability: bool = True
22
) -> None
23
```
24
25
**Description**: The base WebDriver class for remote browser automation.
26
27
**Parameters**:
28
- `command_executor`: URL of the WebDriver server or RemoteConnection instance
29
- `desired_capabilities`: Dictionary of browser capabilities (deprecated)
30
- `browser_profile`: Browser profile (deprecated, use options instead)
31
- `proxy`: Proxy configuration
32
- `keep_alive`: Whether to keep HTTP connection alive
33
- `file_detector`: File detector for handling file uploads
34
- `options`: Browser options object
35
- `service`: Service object for local drivers
36
- `strict_file_interactability`: Whether to enforce strict file interaction
37
38
**Example**:
39
```python
40
from selenium import webdriver
41
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
42
43
# Remote WebDriver
44
driver = webdriver.Remote(
45
command_executor='http://localhost:4444/wd/hub',
46
options=webdriver.ChromeOptions()
47
)
48
```
49
50
## Chrome WebDriver
51
52
{ .api }
53
```python
54
from selenium.webdriver.chrome.webdriver import WebDriver as Chrome
55
56
class WebDriver(ChromiumDriver):
57
def __init__(
58
self,
59
options: Optional[ChromeOptions] = None,
60
service: Optional[ChromeService] = None,
61
keep_alive: bool = True,
62
) -> None
63
```
64
65
**Description**: Controls the ChromeDriver and allows you to drive the Chrome browser.
66
67
**Parameters**:
68
- `options`: Instance of ChromeOptions for browser configuration
69
- `service`: Service object for handling the ChromeDriver process
70
- `keep_alive`: Whether to configure ChromeRemoteConnection to use HTTP keep-alive
71
72
**Example**:
73
```python
74
from selenium import webdriver
75
from selenium.webdriver.chrome.options import Options
76
from selenium.webdriver.chrome.service import Service
77
78
options = Options()
79
options.add_argument('--headless')
80
service = Service('/path/to/chromedriver')
81
82
driver = webdriver.Chrome(options=options, service=service)
83
```
84
85
## Firefox WebDriver
86
87
{ .api }
88
```python
89
from selenium.webdriver.firefox.webdriver import WebDriver as Firefox
90
91
class WebDriver(RemoteWebDriver):
92
CONTEXT_CHROME = "chrome"
93
CONTEXT_CONTENT = "content"
94
95
def __init__(
96
self,
97
options: Optional[FirefoxOptions] = None,
98
service: Optional[FirefoxService] = None,
99
keep_alive: bool = True,
100
) -> None
101
```
102
103
**Description**: Controls the GeckoDriver and allows you to drive the Firefox browser.
104
105
**Parameters**:
106
- `options`: Instance of FirefoxOptions for browser configuration
107
- `service`: Service instance for managing the GeckoDriver process
108
- `keep_alive`: Whether to configure RemoteConnection to use HTTP keep-alive
109
110
**Firefox-Specific Methods**:
111
112
{ .api }
113
```python
114
def set_context(self, context: str) -> None
115
```
116
117
**Description**: Sets the context that Selenium commands are running in.
118
119
**Parameters**:
120
- `context`: Either `CONTEXT_CHROME` or `CONTEXT_CONTENT`
121
122
{ .api }
123
```python
124
@contextmanager
125
def context(self, context: str)
126
```
127
128
**Description**: Context manager for temporarily switching context.
129
130
**Parameters**:
131
- `context`: Either `CONTEXT_CHROME` or `CONTEXT_CONTENT`
132
133
{ .api }
134
```python
135
def install_addon(self, path: str, temporary: bool = False) -> str
136
```
137
138
**Description**: Installs Firefox addon.
139
140
**Parameters**:
141
- `path`: Absolute path to the addon that will be installed
142
- `temporary`: Whether to load the extension temporarily during the session
143
144
**Returns**: Identifier of installed addon
145
146
{ .api }
147
```python
148
def uninstall_addon(self, identifier: str) -> None
149
```
150
151
**Description**: Uninstalls Firefox addon using its identifier.
152
153
**Parameters**:
154
- `identifier`: The addon identifier returned by install_addon()
155
156
{ .api }
157
```python
158
def get_full_page_screenshot_as_file(self, filename: str) -> bool
159
```
160
161
**Description**: Saves a full document screenshot to a PNG file.
162
163
**Parameters**:
164
- `filename`: The full path where to save the screenshot (should end with .png)
165
166
**Returns**: True if successful, False otherwise
167
168
{ .api }
169
```python
170
def get_full_page_screenshot_as_png(self) -> bytes
171
```
172
173
**Description**: Gets the full document screenshot as binary data.
174
175
**Returns**: PNG screenshot data as bytes
176
177
{ .api }
178
```python
179
def get_full_page_screenshot_as_base64(self) -> str
180
```
181
182
**Description**: Gets the full document screenshot as a base64 encoded string.
183
184
**Returns**: Base64 encoded PNG screenshot
185
186
**Example**:
187
```python
188
from selenium import webdriver
189
from selenium.webdriver.firefox.options import Options
190
191
options = Options()
192
options.headless = True
193
driver = webdriver.Firefox(options=options)
194
195
# Firefox-specific features
196
with driver.context(driver.CONTEXT_CHROME):
197
# Chrome context operations
198
pass
199
200
addon_id = driver.install_addon('/path/to/addon.xpi')
201
driver.uninstall_addon(addon_id)
202
```
203
204
## Edge WebDriver
205
206
{ .api }
207
```python
208
from selenium.webdriver.edge.webdriver import WebDriver as Edge
209
210
class WebDriver(ChromiumDriver):
211
def __init__(
212
self,
213
options: Optional[EdgeOptions] = None,
214
service: Optional[EdgeService] = None,
215
keep_alive: bool = True,
216
) -> None
217
```
218
219
**Description**: Controls the EdgeDriver and allows you to drive the Microsoft Edge browser.
220
221
**Parameters**:
222
- `options`: Instance of EdgeOptions for browser configuration
223
- `service`: Service object for handling the EdgeDriver process
224
- `keep_alive`: Whether to configure EdgeRemoteConnection to use HTTP keep-alive
225
226
**Example**:
227
```python
228
from selenium import webdriver
229
from selenium.webdriver.edge.options import Options
230
231
options = Options()
232
options.add_argument('--disable-extensions')
233
driver = webdriver.Edge(options=options)
234
```
235
236
## Safari WebDriver
237
238
{ .api }
239
```python
240
from selenium.webdriver.safari.webdriver import WebDriver as Safari
241
242
class WebDriver(RemoteWebDriver):
243
def __init__(
244
self,
245
options: Optional[SafariOptions] = None,
246
service: Optional[SafariService] = None,
247
keep_alive: bool = True,
248
) -> None
249
```
250
251
**Description**: Controls the SafariDriver and allows you to drive the Safari browser.
252
253
**Parameters**:
254
- `options`: Instance of SafariOptions for browser configuration
255
- `service`: Service instance for managing the SafariDriver process
256
- `keep_alive`: Whether to configure SafariRemoteConnection to use HTTP keep-alive
257
258
**Example**:
259
```python
260
from selenium import webdriver
261
262
driver = webdriver.Safari()
263
```
264
265
## Internet Explorer WebDriver
266
267
{ .api }
268
```python
269
from selenium.webdriver.ie.webdriver import WebDriver as InternetExplorer
270
271
class WebDriver(RemoteWebDriver):
272
def __init__(
273
self,
274
options: Optional[IeOptions] = None,
275
service: Optional[IeService] = None,
276
keep_alive: bool = True,
277
) -> None
278
```
279
280
**Description**: Controls the IEDriver and allows you to drive Internet Explorer.
281
282
**Parameters**:
283
- `options`: Instance of IeOptions for browser configuration
284
- `service`: Service instance for managing the IEDriver process
285
- `keep_alive`: Whether to configure IeRemoteConnection to use HTTP keep-alive
286
287
**Example**:
288
```python
289
from selenium import webdriver
290
from selenium.webdriver.ie.options import Options
291
292
options = Options()
293
options.ignore_protected_mode_settings = True
294
driver = webdriver.Ie(options=options)
295
```
296
297
## WebKitGTK WebDriver
298
299
{ .api }
300
```python
301
from selenium.webdriver.webkitgtk.webdriver import WebDriver as WebKitGTK
302
303
class WebDriver(RemoteWebDriver):
304
def __init__(
305
self,
306
options: Optional[WebKitGTKOptions] = None,
307
service: Optional[WebKitGTKService] = None,
308
keep_alive: bool = True,
309
) -> None
310
```
311
312
**Description**: Controls the WebKitGTKDriver for GTK-based WebKit browsers.
313
314
**Parameters**:
315
- `options`: Instance of WebKitGTKOptions for browser configuration
316
- `service`: Service instance for managing the WebKitGTKDriver process
317
- `keep_alive`: Whether to configure RemoteConnection to use HTTP keep-alive
318
319
## WPEWebKit WebDriver
320
321
{ .api }
322
```python
323
from selenium.webdriver.wpewebkit.webdriver import WebDriver as WPEWebKit
324
325
class WebDriver(RemoteWebDriver):
326
def __init__(
327
self,
328
options: Optional[WPEWebKitOptions] = None,
329
service: Optional[WPEWebKitService] = None,
330
keep_alive: bool = True,
331
) -> None
332
```
333
334
**Description**: Controls the WPEWebKitDriver for WPE WebKit browsers.
335
336
**Parameters**:
337
- `options`: Instance of WPEWebKitOptions for browser configuration
338
- `service`: Service instance for managing the WPEWebKitDriver process
339
- `keep_alive`: Whether to configure RemoteConnection to use HTTP keep-alive
340
341
## Common WebDriver Methods
342
343
All WebDriver classes inherit common methods from the base RemoteWebDriver:
344
345
{ .api }
346
```python
347
def get(self, url: str) -> None
348
```
349
350
**Description**: Loads a web page in the current browser session.
351
352
**Parameters**:
353
- `url`: URL to load
354
355
{ .api }
356
```python
357
def quit(self) -> None
358
```
359
360
**Description**: Quits the driver and closes every associated window.
361
362
{ .api }
363
```python
364
def close(self) -> None
365
```
366
367
**Description**: Closes the current window.
368
369
{ .api }
370
```python
371
def refresh(self) -> None
372
```
373
374
**Description**: Refreshes the current page.
375
376
{ .api }
377
```python
378
def back(self) -> None
379
```
380
381
**Description**: Goes one step backward in the browser history.
382
383
{ .api }
384
```python
385
def forward(self) -> None
386
```
387
388
**Description**: Goes one step forward in the browser history.
389
390
**Example**:
391
```python
392
# Universal pattern for any browser
393
from selenium import webdriver
394
395
# Choose your browser
396
driver = webdriver.Chrome() # or Firefox(), Edge(), Safari(), etc.
397
398
try:
399
driver.get('https://example.com')
400
driver.refresh()
401
driver.back()
402
driver.forward()
403
finally:
404
driver.quit()
405
```
406
407
## Browser Selection Best Practices
408
409
1. **Chrome**: Most commonly used, excellent DevTools support
410
2. **Firefox**: Good for cross-browser testing, supports full-page screenshots
411
3. **Edge**: Windows integration, Chromium-based
412
4. **Safari**: Required for macOS/iOS compatibility testing
413
5. **Remote**: For distributed testing, cloud services, or containerized environments
414
415
**Example of browser selection**:
416
```python
417
import os
418
from selenium import webdriver
419
420
def get_driver(browser_name: str):
421
"""Factory function to get the appropriate WebDriver"""
422
browser_name = browser_name.lower()
423
424
if browser_name == 'chrome':
425
return webdriver.Chrome()
426
elif browser_name == 'firefox':
427
return webdriver.Firefox()
428
elif browser_name == 'edge':
429
return webdriver.Edge()
430
elif browser_name == 'safari':
431
return webdriver.Safari()
432
else:
433
raise ValueError(f"Unsupported browser: {browser_name}")
434
435
# Use environment variable or config
436
browser = os.getenv('BROWSER', 'chrome')
437
driver = get_driver(browser)
438
```