0
# Core WebDriver Interface
1
2
The WebDriver interface provides the primary API for browser automation, offering comprehensive control over browser navigation, element finding, window management, and session control.
3
4
## Capabilities
5
6
### WebDriver Interface
7
8
Main interface for browser automation providing navigation, element finding, window management, and session control.
9
10
```java { .api }
11
/**
12
* WebDriver interface for browser automation
13
* Extends SearchContext for element finding capabilities
14
*/
15
interface WebDriver extends SearchContext {
16
/**
17
* Navigate to the specified URL
18
* @param url - The URL to navigate to
19
*/
20
void get(String url);
21
22
/**
23
* Get the current page URL
24
* @return Current URL as string
25
*/
26
String getCurrentUrl();
27
28
/**
29
* Get the current page title
30
* @return Page title as string
31
*/
32
String getTitle();
33
34
/**
35
* Get the page source HTML
36
* @return Complete page source as string
37
*/
38
String getPageSource();
39
40
/**
41
* Close the browser and end the WebDriver session
42
*/
43
void quit();
44
45
/**
46
* Close the current window/tab
47
*/
48
void close();
49
50
/**
51
* Get all available window handles
52
* @return Set of window handle strings
53
*/
54
Set<String> getWindowHandles();
55
56
/**
57
* Get the current window handle
58
* @return Current window handle string
59
*/
60
String getWindowHandle();
61
62
/**
63
* Get TargetLocator for switching contexts
64
* @return TargetLocator instance
65
*/
66
TargetLocator switchTo();
67
68
/**
69
* Get Navigation interface for browser navigation
70
* @return Navigation instance
71
*/
72
Navigation navigate();
73
74
/**
75
* Get Options interface for browser management
76
* @return Options instance
77
*/
78
Options manage();
79
}
80
```
81
82
**Usage Examples:**
83
84
```java
85
import org.openqa.selenium.WebDriver;
86
import org.openqa.selenium.chrome.ChromeDriver;
87
88
// Create driver instance
89
WebDriver driver = new ChromeDriver();
90
91
try {
92
// Basic navigation
93
driver.get("https://example.com");
94
System.out.println("Current URL: " + driver.getCurrentUrl());
95
System.out.println("Page title: " + driver.getTitle());
96
97
// Window management
98
String mainWindow = driver.getWindowHandle();
99
Set<String> allWindows = driver.getWindowHandles();
100
101
// Navigation using the navigation interface
102
driver.navigate().to("https://another-site.com");
103
driver.navigate().back();
104
driver.navigate().forward();
105
driver.navigate().refresh();
106
107
} finally {
108
driver.quit();
109
}
110
```
111
112
### Navigation Interface
113
114
Browser navigation controls for moving through browser history.
115
116
```java { .api }
117
/**
118
* Navigation interface for browser history control
119
*/
120
interface Navigation {
121
/**
122
* Navigate back in browser history
123
*/
124
void back();
125
126
/**
127
* Navigate forward in browser history
128
*/
129
void forward();
130
131
/**
132
* Navigate to the specified URL
133
* @param url - URL string to navigate to
134
*/
135
void to(String url);
136
137
/**
138
* Navigate to the specified URL
139
* @param url - URL object to navigate to
140
*/
141
void to(URL url);
142
143
/**
144
* Refresh the current page
145
*/
146
void refresh();
147
}
148
```
149
150
### Options Interface
151
152
Browser options and management interface for cookies, timeouts, windows, and logs.
153
154
```java { .api }
155
/**
156
* Options interface for browser management
157
*/
158
interface Options {
159
/**
160
* Add a cookie to the current domain
161
* @param cookie - Cookie to add
162
*/
163
void addCookie(Cookie cookie);
164
165
/**
166
* Delete cookie by name
167
* @param name - Name of cookie to delete
168
*/
169
void deleteCookieNamed(String name);
170
171
/**
172
* Delete specific cookie
173
* @param cookie - Cookie to delete
174
*/
175
void deleteCookie(Cookie cookie);
176
177
/**
178
* Delete all cookies for current domain
179
*/
180
void deleteAllCookies();
181
182
/**
183
* Get all cookies for current domain
184
* @return Set of Cookie objects
185
*/
186
Set<Cookie> getCookies();
187
188
/**
189
* Get cookie by name
190
* @param name - Cookie name
191
* @return Cookie object or null if not found
192
*/
193
Cookie getCookieNamed(String name);
194
195
/**
196
* Get Timeouts interface for timeout configuration
197
* @return Timeouts instance
198
*/
199
Timeouts timeouts();
200
201
/**
202
* Get Window interface for window management
203
* @return Window instance
204
*/
205
Window window();
206
207
/**
208
* Get Logs interface for log management
209
* @return Logs instance
210
*/
211
Logs logs();
212
}
213
```
214
215
### Timeouts Interface
216
217
Timeout configuration for various WebDriver operations.
218
219
```java { .api }
220
/**
221
* Timeouts interface for configuring various operation timeouts
222
*/
223
interface Timeouts {
224
/**
225
* Set implicit wait timeout for element finding
226
* @param timeout - Duration to wait
227
* @return Timeouts instance for chaining
228
*/
229
Timeouts implicitlyWait(Duration timeout);
230
231
/**
232
* Set script execution timeout for async JavaScript
233
* @param timeout - Duration to wait for scripts
234
* @return Timeouts instance for chaining
235
*/
236
Timeouts setScriptTimeout(Duration timeout);
237
238
/**
239
* Set page load timeout
240
* @param timeout - Duration to wait for page loads
241
* @return Timeouts instance for chaining
242
*/
243
Timeouts pageLoadTimeout(Duration timeout);
244
}
245
```
246
247
### Window Interface
248
249
Window positioning, sizing, and state control.
250
251
```java { .api }
252
/**
253
* Window interface for window positioning and sizing
254
*/
255
interface Window {
256
/**
257
* Get current window size
258
* @return Dimension object with width and height
259
*/
260
Dimension getSize();
261
262
/**
263
* Set window size
264
* @param targetSize - Desired window dimensions
265
*/
266
void setSize(Dimension targetSize);
267
268
/**
269
* Get current window position
270
* @return Point object with x,y coordinates
271
*/
272
Point getPosition();
273
274
/**
275
* Set window position
276
* @param targetPosition - Desired window position
277
*/
278
void setPosition(Point targetPosition);
279
280
/**
281
* Maximize the current window
282
*/
283
void maximize();
284
285
/**
286
* Minimize the current window
287
*/
288
void minimize();
289
290
/**
291
* Enter fullscreen mode
292
*/
293
void fullscreen();
294
}
295
```
296
297
### TargetLocator Interface
298
299
Context switching for frames, windows, and alert dialogs.
300
301
```java { .api }
302
/**
303
* TargetLocator interface for switching between different browser contexts
304
*/
305
interface TargetLocator {
306
/**
307
* Switch to frame by index
308
* @param index - Frame index (0-based)
309
* @return WebDriver instance focused on the frame
310
*/
311
WebDriver frame(int index);
312
313
/**
314
* Switch to frame by name or ID
315
* @param nameOrId - Frame name or ID attribute
316
* @return WebDriver instance focused on the frame
317
*/
318
WebDriver frame(String nameOrId);
319
320
/**
321
* Switch to frame by WebElement
322
* @param frameElement - WebElement representing the frame
323
* @return WebDriver instance focused on the frame
324
*/
325
WebDriver frame(WebElement frameElement);
326
327
/**
328
* Switch to parent frame
329
* @return WebDriver instance focused on parent frame
330
*/
331
WebDriver parentFrame();
332
333
/**
334
* Switch to window by name or handle
335
* @param nameOrHandle - Window name or handle
336
* @return WebDriver instance focused on the window
337
*/
338
WebDriver window(String nameOrHandle);
339
340
/**
341
* Open new window or tab
342
* @param typeHint - Type of window to open (TAB or WINDOW)
343
* @return WebDriver instance focused on new window
344
*/
345
WebDriver newWindow(WindowType typeHint);
346
347
/**
348
* Switch to main document content (out of all frames)
349
* @return WebDriver instance focused on main content
350
*/
351
WebDriver defaultContent();
352
353
/**
354
* Get currently focused element
355
* @return WebElement that currently has focus
356
*/
357
WebElement activeElement();
358
359
/**
360
* Switch to alert dialog
361
* @return Alert instance for interacting with alert
362
*/
363
Alert alert();
364
}
365
```
366
367
**Usage Examples:**
368
369
```java
370
import org.openqa.selenium.WebDriver;
371
import org.openqa.selenium.chrome.ChromeDriver;
372
import org.openqa.selenium.support.ui.WebDriverWait;
373
import org.openqa.selenium.support.ui.ExpectedConditions;
374
import java.time.Duration;
375
376
WebDriver driver = new ChromeDriver();
377
378
try {
379
// Configure timeouts
380
driver.manage().timeouts()
381
.implicitlyWait(Duration.ofSeconds(10))
382
.pageLoadTimeout(Duration.ofSeconds(30))
383
.setScriptTimeout(Duration.ofSeconds(15));
384
385
// Window management
386
driver.manage().window().maximize();
387
driver.manage().window().setSize(new Dimension(1920, 1080));
388
389
// Cookie management
390
driver.get("https://example.com");
391
driver.manage().addCookie(new Cookie("session", "abc123"));
392
Set<Cookie> cookies = driver.manage().getCookies();
393
394
// Frame switching
395
driver.get("https://example.com/frames");
396
driver.switchTo().frame("contentFrame");
397
// ... interact with frame content
398
driver.switchTo().defaultContent();
399
400
// Window switching
401
driver.switchTo().newWindow(WindowType.TAB);
402
driver.get("https://another-site.com");
403
String newTab = driver.getWindowHandle();
404
405
// Switch back to original window
406
Set<String> handles = driver.getWindowHandles();
407
for (String handle : handles) {
408
if (!handle.equals(newTab)) {
409
driver.switchTo().window(handle);
410
break;
411
}
412
}
413
414
// Alert handling
415
WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(10));
416
Alert alert = wait.until(ExpectedConditions.alertIsPresent());
417
alert.accept();
418
419
} finally {
420
driver.quit();
421
}
422
```