0
# Browser Drivers
1
2
Browser-specific WebDriver implementations with their corresponding options classes for configuration and capabilities management.
3
4
## Capabilities
5
6
### ChromeDriver
7
8
WebDriver implementation for Google Chrome with Chrome-specific capabilities and DevTools integration.
9
10
```java { .api }
11
/**
12
* ChromeDriver for Google Chrome browser automation
13
* Implements WebDriver with additional Chrome-specific capabilities
14
*/
15
class ChromeDriver implements WebDriver, JavascriptExecutor, TakesScreenshot, HasAuthentication, HasBiDi, HasCasting, HasCdp, HasLaunching, HasLogEvents, HasNetworkConditions, HasPermissions, HasDownloads, HasDevTools {
16
/**
17
* Create ChromeDriver with default configuration
18
*/
19
ChromeDriver();
20
21
/**
22
* Create ChromeDriver with custom options
23
* @param options - ChromeOptions for configuration
24
*/
25
ChromeDriver(ChromeOptions options);
26
27
/**
28
* Create ChromeDriver with custom service
29
* @param service - ChromeDriverService for driver management
30
*/
31
ChromeDriver(ChromeDriverService service);
32
33
/**
34
* Create ChromeDriver with service and options
35
* @param service - ChromeDriverService for driver management
36
* @param options - ChromeOptions for configuration
37
*/
38
ChromeDriver(ChromeDriverService service, ChromeOptions options);
39
}
40
```
41
42
### ChromeOptions
43
44
Configuration options for Chrome browser including arguments, extensions, and experimental features.
45
46
```java { .api }
47
/**
48
* ChromeOptions for configuring Chrome browser behavior
49
* Extends MutableCapabilities with Chrome-specific options
50
*/
51
class ChromeOptions extends MutableCapabilities {
52
/**
53
* Create ChromeOptions with default settings
54
*/
55
ChromeOptions();
56
57
/**
58
* Add Chrome command line arguments
59
* @param arguments - Chrome arguments to add
60
* @return ChromeOptions instance for chaining
61
*/
62
ChromeOptions addArguments(String... arguments);
63
64
/**
65
* Add Chrome command line arguments from list
66
* @param arguments - List of Chrome arguments
67
* @return ChromeOptions instance for chaining
68
*/
69
ChromeOptions addArguments(List<String> arguments);
70
71
/**
72
* Add Chrome extensions from file paths
73
* @param paths - File paths to Chrome extension files
74
* @return ChromeOptions instance for chaining
75
*/
76
ChromeOptions addExtensions(File... paths);
77
78
/**
79
* Add Chrome extensions from file list
80
* @param extensions - List of extension files
81
* @return ChromeOptions instance for chaining
82
*/
83
ChromeOptions addExtensions(List<File> extensions);
84
85
/**
86
* Add encoded Chrome extensions
87
* @param encoded - Base64 encoded extension strings
88
* @return ChromeOptions instance for chaining
89
*/
90
ChromeOptions addEncodedExtensions(String... encoded);
91
92
/**
93
* Set Chrome experimental option
94
* @param name - Option name
95
* @param value - Option value
96
* @return ChromeOptions instance for chaining
97
*/
98
ChromeOptions setExperimentalOption(String name, Object value);
99
100
/**
101
* Set headless mode
102
* @param headless - true to enable headless mode
103
* @return ChromeOptions instance for chaining
104
*/
105
ChromeOptions setHeadless(boolean headless);
106
107
/**
108
* Set Chrome binary path
109
* @param path - Path to Chrome executable
110
* @return ChromeOptions instance for chaining
111
*/
112
ChromeOptions setBinary(String path);
113
114
/**
115
* Set Chrome binary file
116
* @param path - File pointing to Chrome executable
117
* @return ChromeOptions instance for chaining
118
*/
119
ChromeOptions setBinary(File path);
120
}
121
```
122
123
### FirefoxDriver
124
125
WebDriver implementation for Mozilla Firefox with Firefox-specific capabilities.
126
127
```java { .api }
128
/**
129
* FirefoxDriver for Mozilla Firefox browser automation
130
* Implements WebDriver with Firefox-specific capabilities
131
*/
132
class FirefoxDriver implements WebDriver, JavascriptExecutor, TakesScreenshot, HasAuthentication, HasFullPageScreenshot, HasContext {
133
/**
134
* Create FirefoxDriver with default configuration
135
*/
136
FirefoxDriver();
137
138
/**
139
* Create FirefoxDriver with custom options
140
* @param options - FirefoxOptions for configuration
141
*/
142
FirefoxDriver(FirefoxOptions options);
143
144
/**
145
* Create FirefoxDriver with custom service
146
* @param service - GeckoDriverService for driver management
147
*/
148
FirefoxDriver(GeckoDriverService service);
149
150
/**
151
* Create FirefoxDriver with service and options
152
* @param service - GeckoDriverService for driver management
153
* @param options - FirefoxOptions for configuration
154
*/
155
FirefoxDriver(GeckoDriverService service, FirefoxOptions options);
156
}
157
```
158
159
### FirefoxOptions
160
161
Configuration options for Firefox browser including arguments, profile, and preferences.
162
163
```java { .api }
164
/**
165
* FirefoxOptions for configuring Firefox browser behavior
166
* Extends MutableCapabilities with Firefox-specific options
167
*/
168
class FirefoxOptions extends MutableCapabilities {
169
/**
170
* Create FirefoxOptions with default settings
171
*/
172
FirefoxOptions();
173
174
/**
175
* Add Firefox command line arguments
176
* @param arguments - Firefox arguments to add
177
* @return FirefoxOptions instance for chaining
178
*/
179
FirefoxOptions addArguments(String... arguments);
180
181
/**
182
* Add Firefox command line arguments from list
183
* @param arguments - List of Firefox arguments
184
* @return FirefoxOptions instance for chaining
185
*/
186
FirefoxOptions addArguments(List<String> arguments);
187
188
/**
189
* Set headless mode
190
* @param headless - true to enable headless mode
191
* @return FirefoxOptions instance for chaining
192
*/
193
FirefoxOptions setHeadless(boolean headless);
194
195
/**
196
* Set Firefox binary path
197
* @param path - Path to Firefox executable
198
* @return FirefoxOptions instance for chaining
199
*/
200
FirefoxOptions setBinary(String path);
201
202
/**
203
* Set Firefox binary file
204
* @param path - File pointing to Firefox executable
205
* @return FirefoxOptions instance for chaining
206
*/
207
FirefoxOptions setBinary(File path);
208
209
/**
210
* Set Firefox profile
211
* @param profile - FirefoxProfile for browser configuration
212
* @return FirefoxOptions instance for chaining
213
*/
214
FirefoxOptions setProfile(FirefoxProfile profile);
215
216
/**
217
* Set log level for browser logs
218
* @param logLevel - Log level (e.g., Level.INFO)
219
* @return FirefoxOptions instance for chaining
220
*/
221
FirefoxOptions setLogLevel(Level logLevel);
222
}
223
```
224
225
### EdgeDriver
226
227
WebDriver implementation for Microsoft Edge with Edge-specific capabilities.
228
229
```java { .api }
230
/**
231
* EdgeDriver for Microsoft Edge browser automation
232
* Implements WebDriver with Edge-specific capabilities (Chromium-based)
233
*/
234
class EdgeDriver implements WebDriver, JavascriptExecutor, TakesScreenshot, HasAuthentication, HasBiDi, HasCasting, HasCdp, HasLaunching, HasLogEvents, HasNetworkConditions, HasPermissions, HasDownloads, HasDevTools {
235
/**
236
* Create EdgeDriver with default configuration
237
*/
238
EdgeDriver();
239
240
/**
241
* Create EdgeDriver with custom options
242
* @param options - EdgeOptions for configuration
243
*/
244
EdgeDriver(EdgeOptions options);
245
246
/**
247
* Create EdgeDriver with custom service
248
* @param service - EdgeDriverService for driver management
249
*/
250
EdgeDriver(EdgeDriverService service);
251
252
/**
253
* Create EdgeDriver with service and options
254
* @param service - EdgeDriverService for driver management
255
* @param options - EdgeOptions for configuration
256
*/
257
EdgeDriver(EdgeDriverService service, EdgeOptions options);
258
}
259
```
260
261
### SafariDriver
262
263
WebDriver implementation for Apple Safari with Safari-specific capabilities.
264
265
```java { .api }
266
/**
267
* SafariDriver for Apple Safari browser automation
268
* Implements WebDriver with Safari-specific capabilities
269
*/
270
class SafariDriver implements WebDriver, JavascriptExecutor, TakesScreenshot, HasAuthentication, HasPermissions, HasDebugger {
271
/**
272
* Create SafariDriver with default configuration
273
*/
274
SafariDriver();
275
276
/**
277
* Create SafariDriver with custom options
278
* @param options - SafariOptions for configuration
279
*/
280
SafariDriver(SafariOptions options);
281
282
/**
283
* Create SafariDriver with custom service
284
* @param service - SafariDriverService for driver management
285
*/
286
SafariDriver(SafariDriverService service);
287
288
/**
289
* Create SafariDriver with service and options
290
* @param service - SafariDriverService for driver management
291
* @param options - SafariOptions for configuration
292
*/
293
SafariDriver(SafariDriverService service, SafariOptions options);
294
}
295
```
296
297
## Usage Examples
298
299
### Chrome Driver Setup and Configuration
300
301
```java
302
import org.openqa.selenium.WebDriver;
303
import org.openqa.selenium.chrome.ChromeDriver;
304
import org.openqa.selenium.chrome.ChromeOptions;
305
import org.openqa.selenium.chrome.ChromeDriverService;
306
import java.io.File;
307
308
// Basic Chrome setup
309
WebDriver driver = new ChromeDriver();
310
311
// Chrome with custom options
312
ChromeOptions options = new ChromeOptions();
313
options.addArguments("--headless"); // Run in headless mode
314
options.addArguments("--no-sandbox");
315
options.addArguments("--disable-dev-shm-usage");
316
options.addArguments("--window-size=1920,1080");
317
options.addArguments("--disable-gpu");
318
options.addArguments("--disable-extensions");
319
320
WebDriver headlessDriver = new ChromeDriver(options);
321
322
// Chrome with extensions
323
ChromeOptions extOptions = new ChromeOptions();
324
extOptions.addExtensions(new File("path/to/extension.crx"));
325
extOptions.addExtensions(new File("path/to/another-extension.crx"));
326
327
WebDriver driverWithExtensions = new ChromeDriver(extOptions);
328
329
// Chrome with experimental options
330
ChromeOptions expOptions = new ChromeOptions();
331
expOptions.setExperimentalOption("useAutomationExtension", false);
332
expOptions.setExperimentalOption("excludeSwitches", Arrays.asList("enable-automation"));
333
expOptions.setExperimentalOption("detach", true);
334
335
// Chrome with custom binary
336
expOptions.setBinary("/path/to/custom/chrome");
337
338
WebDriver customChromeDriver = new ChromeDriver(expOptions);
339
```
340
341
### Chrome Performance and Mobile Emulation
342
343
```java
344
// Mobile emulation
345
ChromeOptions mobileOptions = new ChromeOptions();
346
Map<String, Object> mobileEmulation = new HashMap<>();
347
mobileEmulation.put("deviceName", "iPhone X");
348
mobileOptions.setExperimentalOption("mobileEmulation", mobileEmulation);
349
350
// Custom mobile metrics
351
Map<String, Object> deviceMetrics = new HashMap<>();
352
deviceMetrics.put("width", 375);
353
deviceMetrics.put("height", 812);
354
deviceMetrics.put("pixelRatio", 3.0);
355
mobileEmulation.put("deviceMetrics", deviceMetrics);
356
mobileEmulation.put("userAgent", "Mozilla/5.0 (iPhone; CPU iPhone OS 11_0 like Mac OS X) AppleWebKit/604.1.38");
357
mobileOptions.setExperimentalOption("mobileEmulation", mobileEmulation);
358
359
WebDriver mobileDriver = new ChromeDriver(mobileOptions);
360
361
// Performance logging
362
ChromeOptions perfOptions = new ChromeOptions();
363
LoggingPreferences logPrefs = new LoggingPreferences();
364
logPrefs.enable(LogType.PERFORMANCE, Level.INFO);
365
perfOptions.setCapability(CapabilityType.LOGGING_PREFS, logPrefs);
366
367
WebDriver perfDriver = new ChromeDriver(perfOptions);
368
```
369
370
### Firefox Driver Setup and Configuration
371
372
```java
373
import org.openqa.selenium.firefox.FirefoxDriver;
374
import org.openqa.selenium.firefox.FirefoxOptions;
375
import org.openqa.selenium.firefox.FirefoxProfile;
376
import org.openqa.selenium.firefox.GeckoDriverService;
377
378
// Basic Firefox setup
379
WebDriver firefoxDriver = new FirefoxDriver();
380
381
// Firefox with options
382
FirefoxOptions firefoxOptions = new FirefoxOptions();
383
firefoxOptions.setHeadless(true);
384
firefoxOptions.addArguments("--width=1920");
385
firefoxOptions.addArguments("--height=1080");
386
387
WebDriver headlessFirefox = new FirefoxDriver(firefoxOptions);
388
389
// Firefox with custom profile
390
FirefoxProfile profile = new FirefoxProfile();
391
profile.setPreference("browser.download.folderList", 2);
392
profile.setPreference("browser.download.dir", "/path/to/downloads");
393
profile.setPreference("browser.helperApps.neverAsk.saveToDisk", "application/pdf,text/csv");
394
profile.setPreference("pdfjs.disabled", true);
395
396
FirefoxOptions profileOptions = new FirefoxOptions();
397
profileOptions.setProfile(profile);
398
399
WebDriver profileFirefox = new FirefoxDriver(profileOptions);
400
401
// Firefox with custom binary
402
firefoxOptions.setBinary("/path/to/firefox");
403
```
404
405
### Edge Driver Setup
406
407
```java
408
import org.openqa.selenium.edge.EdgeDriver;
409
import org.openqa.selenium.edge.EdgeOptions;
410
411
// Basic Edge setup
412
WebDriver edgeDriver = new EdgeDriver();
413
414
// Edge with options (similar to Chrome since it's Chromium-based)
415
EdgeOptions edgeOptions = new EdgeOptions();
416
edgeOptions.addArguments("--headless");
417
edgeOptions.addArguments("--disable-gpu");
418
edgeOptions.addArguments("--window-size=1920,1080");
419
420
WebDriver headlessEdge = new EdgeDriver(edgeOptions);
421
422
// Edge with experimental options
423
edgeOptions.setExperimentalOption("useAutomationExtension", false);
424
edgeOptions.setExperimentalOption("excludeSwitches", Arrays.asList("enable-automation"));
425
```
426
427
### Safari Driver Setup
428
429
```java
430
import org.openqa.selenium.safari.SafariDriver;
431
import org.openqa.selenium.safari.SafariOptions;
432
433
// Basic Safari setup (macOS only)
434
WebDriver safariDriver = new SafariDriver();
435
436
// Safari with options
437
SafariOptions safariOptions = new SafariOptions();
438
safariOptions.setUseTechnologyPreview(true); // Use Safari Technology Preview
439
safariOptions.setAutomaticInspection(false);
440
safariOptions.setAutomaticProfiling(false);
441
442
WebDriver tpSafari = new SafariDriver(safariOptions);
443
```
444
445
### Driver Service Configuration
446
447
```java
448
import org.openqa.selenium.chrome.ChromeDriverService;
449
import org.openqa.selenium.firefox.GeckoDriverService;
450
import java.io.File;
451
452
// Chrome service configuration
453
ChromeDriverService chromeService = new ChromeDriverService.Builder()
454
.usingDriverExecutable(new File("/path/to/chromedriver"))
455
.usingAnyFreePort()
456
.withEnvironment(Map.of("DISPLAY", ":0"))
457
.withLogLevel(ChromeDriverService.LOG_LEVEL_ALL)
458
.withSilent(false)
459
.withVerbose(true)
460
.build();
461
462
ChromeOptions chromeOptions = new ChromeOptions();
463
WebDriver chromeDriverWithService = new ChromeDriver(chromeService, chromeOptions);
464
465
// Firefox service configuration
466
GeckoDriverService geckoService = new GeckoDriverService.Builder()
467
.usingDriverExecutable(new File("/path/to/geckodriver"))
468
.usingAnyFreePort()
469
.withLogLevel(Level.INFO)
470
.build();
471
472
FirefoxOptions firefoxOptions = new FirefoxOptions();
473
WebDriver firefoxDriverWithService = new FirefoxDriver(geckoService, firefoxOptions);
474
```
475
476
### Cross-Browser Setup Pattern
477
478
```java
479
public WebDriver createDriver(String browserName) {
480
WebDriver driver;
481
482
switch (browserName.toLowerCase()) {
483
case "chrome":
484
ChromeOptions chromeOptions = new ChromeOptions();
485
chromeOptions.addArguments("--disable-blink-features=AutomationControlled");
486
chromeOptions.setExperimentalOption("useAutomationExtension", false);
487
chromeOptions.setExperimentalOption("excludeSwitches", Arrays.asList("enable-automation"));
488
driver = new ChromeDriver(chromeOptions);
489
break;
490
491
case "firefox":
492
FirefoxOptions firefoxOptions = new FirefoxOptions();
493
firefoxOptions.addArguments("--width=1920");
494
firefoxOptions.addArguments("--height=1080");
495
driver = new FirefoxDriver(firefoxOptions);
496
break;
497
498
case "edge":
499
EdgeOptions edgeOptions = new EdgeOptions();
500
edgeOptions.addArguments("--disable-blink-features=AutomationControlled");
501
driver = new EdgeDriver(edgeOptions);
502
break;
503
504
case "safari":
505
SafariOptions safariOptions = new SafariOptions();
506
driver = new SafariDriver(safariOptions);
507
break;
508
509
default:
510
throw new IllegalArgumentException("Browser not supported: " + browserName);
511
}
512
513
// Common configuration
514
driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(10));
515
driver.manage().window().maximize();
516
517
return driver;
518
}
519
520
// Usage
521
WebDriver driver = createDriver("chrome");
522
```
523
524
### Advanced Browser Configuration
525
526
```java
527
// Chrome with custom user data directory
528
ChromeOptions advancedOptions = new ChromeOptions();
529
advancedOptions.addArguments("--user-data-dir=/path/to/user/data");
530
advancedOptions.addArguments("--profile-directory=Profile 1");
531
532
// Disable images and CSS for faster loading
533
advancedOptions.addArguments("--blink-settings=imagesEnabled=false");
534
advancedOptions.setExperimentalOption("prefs", Map.of(
535
"profile.managed_default_content_settings.images", 2,
536
"profile.default_content_setting_values.stylesheet", 2
537
));
538
539
// Chrome with proxy
540
Proxy proxy = new Proxy();
541
proxy.setHttpProxy("proxy.company.com:8080");
542
proxy.setSslProxy("proxy.company.com:8080");
543
advancedOptions.setCapability(CapabilityType.PROXY, proxy);
544
545
// Chrome with download preferences
546
Map<String, Object> prefs = new HashMap<>();
547
prefs.put("download.default_directory", "/path/to/downloads");
548
prefs.put("download.prompt_for_download", false);
549
prefs.put("plugins.always_open_pdf_externally", true);
550
advancedOptions.setExperimentalOption("prefs", prefs);
551
552
WebDriver advancedDriver = new ChromeDriver(advancedOptions);
553
```
554
555
### Browser Cleanup and Resource Management
556
557
```java
558
// Proper driver cleanup
559
public void cleanupDriver(WebDriver driver) {
560
if (driver != null) {
561
try {
562
// Close all windows
563
driver.quit();
564
} catch (Exception e) {
565
System.err.println("Error during driver cleanup: " + e.getMessage());
566
}
567
}
568
}
569
570
// Try-with-resources pattern (if driver implements AutoCloseable)
571
public void useDriverSafely() {
572
ChromeOptions options = new ChromeOptions();
573
options.setHeadless(true);
574
575
try (ChromeDriver driver = new ChromeDriver(options)) {
576
driver.get("https://example.com");
577
// Perform automation tasks
578
579
} catch (Exception e) {
580
System.err.println("Error during automation: " + e.getMessage());
581
}
582
// Driver automatically closed
583
}
584
585
// Shutdown hook for cleanup
586
Runtime.getRuntime().addShutdownHook(new Thread(() -> {
587
if (driver != null) {
588
driver.quit();
589
}
590
}));
591
```