Selenium WebDriver core API for automating web browsers across different platforms and programming languages.
—
The WebDriver interface provides the primary API for browser automation, offering comprehensive control over browser navigation, element finding, window management, and session control.
Main interface for browser automation providing navigation, element finding, window management, and session control.
/**
* WebDriver interface for browser automation
* Extends SearchContext for element finding capabilities
*/
interface WebDriver extends SearchContext {
/**
* Navigate to the specified URL
* @param url - The URL to navigate to
*/
void get(String url);
/**
* Get the current page URL
* @return Current URL as string
*/
String getCurrentUrl();
/**
* Get the current page title
* @return Page title as string
*/
String getTitle();
/**
* Get the page source HTML
* @return Complete page source as string
*/
String getPageSource();
/**
* Close the browser and end the WebDriver session
*/
void quit();
/**
* Close the current window/tab
*/
void close();
/**
* Get all available window handles
* @return Set of window handle strings
*/
Set<String> getWindowHandles();
/**
* Get the current window handle
* @return Current window handle string
*/
String getWindowHandle();
/**
* Get TargetLocator for switching contexts
* @return TargetLocator instance
*/
TargetLocator switchTo();
/**
* Get Navigation interface for browser navigation
* @return Navigation instance
*/
Navigation navigate();
/**
* Get Options interface for browser management
* @return Options instance
*/
Options manage();
}Usage Examples:
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
// Create driver instance
WebDriver driver = new ChromeDriver();
try {
// Basic navigation
driver.get("https://example.com");
System.out.println("Current URL: " + driver.getCurrentUrl());
System.out.println("Page title: " + driver.getTitle());
// Window management
String mainWindow = driver.getWindowHandle();
Set<String> allWindows = driver.getWindowHandles();
// Navigation using the navigation interface
driver.navigate().to("https://another-site.com");
driver.navigate().back();
driver.navigate().forward();
driver.navigate().refresh();
} finally {
driver.quit();
}Browser navigation controls for moving through browser history.
/**
* Navigation interface for browser history control
*/
interface Navigation {
/**
* Navigate back in browser history
*/
void back();
/**
* Navigate forward in browser history
*/
void forward();
/**
* Navigate to the specified URL
* @param url - URL string to navigate to
*/
void to(String url);
/**
* Navigate to the specified URL
* @param url - URL object to navigate to
*/
void to(URL url);
/**
* Refresh the current page
*/
void refresh();
}Browser options and management interface for cookies, timeouts, windows, and logs.
/**
* Options interface for browser management
*/
interface Options {
/**
* Add a cookie to the current domain
* @param cookie - Cookie to add
*/
void addCookie(Cookie cookie);
/**
* Delete cookie by name
* @param name - Name of cookie to delete
*/
void deleteCookieNamed(String name);
/**
* Delete specific cookie
* @param cookie - Cookie to delete
*/
void deleteCookie(Cookie cookie);
/**
* Delete all cookies for current domain
*/
void deleteAllCookies();
/**
* Get all cookies for current domain
* @return Set of Cookie objects
*/
Set<Cookie> getCookies();
/**
* Get cookie by name
* @param name - Cookie name
* @return Cookie object or null if not found
*/
Cookie getCookieNamed(String name);
/**
* Get Timeouts interface for timeout configuration
* @return Timeouts instance
*/
Timeouts timeouts();
/**
* Get Window interface for window management
* @return Window instance
*/
Window window();
/**
* Get Logs interface for log management
* @return Logs instance
*/
Logs logs();
}Timeout configuration for various WebDriver operations.
/**
* Timeouts interface for configuring various operation timeouts
*/
interface Timeouts {
/**
* Set implicit wait timeout for element finding
* @param timeout - Duration to wait
* @return Timeouts instance for chaining
*/
Timeouts implicitlyWait(Duration timeout);
/**
* Set script execution timeout for async JavaScript
* @param timeout - Duration to wait for scripts
* @return Timeouts instance for chaining
*/
Timeouts setScriptTimeout(Duration timeout);
/**
* Set page load timeout
* @param timeout - Duration to wait for page loads
* @return Timeouts instance for chaining
*/
Timeouts pageLoadTimeout(Duration timeout);
}Window positioning, sizing, and state control.
/**
* Window interface for window positioning and sizing
*/
interface Window {
/**
* Get current window size
* @return Dimension object with width and height
*/
Dimension getSize();
/**
* Set window size
* @param targetSize - Desired window dimensions
*/
void setSize(Dimension targetSize);
/**
* Get current window position
* @return Point object with x,y coordinates
*/
Point getPosition();
/**
* Set window position
* @param targetPosition - Desired window position
*/
void setPosition(Point targetPosition);
/**
* Maximize the current window
*/
void maximize();
/**
* Minimize the current window
*/
void minimize();
/**
* Enter fullscreen mode
*/
void fullscreen();
}Context switching for frames, windows, and alert dialogs.
/**
* TargetLocator interface for switching between different browser contexts
*/
interface TargetLocator {
/**
* Switch to frame by index
* @param index - Frame index (0-based)
* @return WebDriver instance focused on the frame
*/
WebDriver frame(int index);
/**
* Switch to frame by name or ID
* @param nameOrId - Frame name or ID attribute
* @return WebDriver instance focused on the frame
*/
WebDriver frame(String nameOrId);
/**
* Switch to frame by WebElement
* @param frameElement - WebElement representing the frame
* @return WebDriver instance focused on the frame
*/
WebDriver frame(WebElement frameElement);
/**
* Switch to parent frame
* @return WebDriver instance focused on parent frame
*/
WebDriver parentFrame();
/**
* Switch to window by name or handle
* @param nameOrHandle - Window name or handle
* @return WebDriver instance focused on the window
*/
WebDriver window(String nameOrHandle);
/**
* Open new window or tab
* @param typeHint - Type of window to open (TAB or WINDOW)
* @return WebDriver instance focused on new window
*/
WebDriver newWindow(WindowType typeHint);
/**
* Switch to main document content (out of all frames)
* @return WebDriver instance focused on main content
*/
WebDriver defaultContent();
/**
* Get currently focused element
* @return WebElement that currently has focus
*/
WebElement activeElement();
/**
* Switch to alert dialog
* @return Alert instance for interacting with alert
*/
Alert alert();
}Usage Examples:
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.openqa.selenium.support.ui.ExpectedConditions;
import java.time.Duration;
WebDriver driver = new ChromeDriver();
try {
// Configure timeouts
driver.manage().timeouts()
.implicitlyWait(Duration.ofSeconds(10))
.pageLoadTimeout(Duration.ofSeconds(30))
.setScriptTimeout(Duration.ofSeconds(15));
// Window management
driver.manage().window().maximize();
driver.manage().window().setSize(new Dimension(1920, 1080));
// Cookie management
driver.get("https://example.com");
driver.manage().addCookie(new Cookie("session", "abc123"));
Set<Cookie> cookies = driver.manage().getCookies();
// Frame switching
driver.get("https://example.com/frames");
driver.switchTo().frame("contentFrame");
// ... interact with frame content
driver.switchTo().defaultContent();
// Window switching
driver.switchTo().newWindow(WindowType.TAB);
driver.get("https://another-site.com");
String newTab = driver.getWindowHandle();
// Switch back to original window
Set<String> handles = driver.getWindowHandles();
for (String handle : handles) {
if (!handle.equals(newTab)) {
driver.switchTo().window(handle);
break;
}
}
// Alert handling
WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(10));
Alert alert = wait.until(ExpectedConditions.alertIsPresent());
alert.accept();
} finally {
driver.quit();
}Install with Tessl CLI
npx tessl i tessl/maven-org-seleniumhq-selenium--selenium-api