Selenium WebDriver core API for automating web browsers across different platforms and programming languages.
—
Browser-specific WebDriver implementations with their corresponding options classes for configuration and capabilities management.
WebDriver implementation for Google Chrome with Chrome-specific capabilities and DevTools integration.
/**
* ChromeDriver for Google Chrome browser automation
* Implements WebDriver with additional Chrome-specific capabilities
*/
class ChromeDriver implements WebDriver, JavascriptExecutor, TakesScreenshot, HasAuthentication, HasBiDi, HasCasting, HasCdp, HasLaunching, HasLogEvents, HasNetworkConditions, HasPermissions, HasDownloads, HasDevTools {
/**
* Create ChromeDriver with default configuration
*/
ChromeDriver();
/**
* Create ChromeDriver with custom options
* @param options - ChromeOptions for configuration
*/
ChromeDriver(ChromeOptions options);
/**
* Create ChromeDriver with custom service
* @param service - ChromeDriverService for driver management
*/
ChromeDriver(ChromeDriverService service);
/**
* Create ChromeDriver with service and options
* @param service - ChromeDriverService for driver management
* @param options - ChromeOptions for configuration
*/
ChromeDriver(ChromeDriverService service, ChromeOptions options);
}Configuration options for Chrome browser including arguments, extensions, and experimental features.
/**
* ChromeOptions for configuring Chrome browser behavior
* Extends MutableCapabilities with Chrome-specific options
*/
class ChromeOptions extends MutableCapabilities {
/**
* Create ChromeOptions with default settings
*/
ChromeOptions();
/**
* Add Chrome command line arguments
* @param arguments - Chrome arguments to add
* @return ChromeOptions instance for chaining
*/
ChromeOptions addArguments(String... arguments);
/**
* Add Chrome command line arguments from list
* @param arguments - List of Chrome arguments
* @return ChromeOptions instance for chaining
*/
ChromeOptions addArguments(List<String> arguments);
/**
* Add Chrome extensions from file paths
* @param paths - File paths to Chrome extension files
* @return ChromeOptions instance for chaining
*/
ChromeOptions addExtensions(File... paths);
/**
* Add Chrome extensions from file list
* @param extensions - List of extension files
* @return ChromeOptions instance for chaining
*/
ChromeOptions addExtensions(List<File> extensions);
/**
* Add encoded Chrome extensions
* @param encoded - Base64 encoded extension strings
* @return ChromeOptions instance for chaining
*/
ChromeOptions addEncodedExtensions(String... encoded);
/**
* Set Chrome experimental option
* @param name - Option name
* @param value - Option value
* @return ChromeOptions instance for chaining
*/
ChromeOptions setExperimentalOption(String name, Object value);
/**
* Set headless mode
* @param headless - true to enable headless mode
* @return ChromeOptions instance for chaining
*/
ChromeOptions setHeadless(boolean headless);
/**
* Set Chrome binary path
* @param path - Path to Chrome executable
* @return ChromeOptions instance for chaining
*/
ChromeOptions setBinary(String path);
/**
* Set Chrome binary file
* @param path - File pointing to Chrome executable
* @return ChromeOptions instance for chaining
*/
ChromeOptions setBinary(File path);
}WebDriver implementation for Mozilla Firefox with Firefox-specific capabilities.
/**
* FirefoxDriver for Mozilla Firefox browser automation
* Implements WebDriver with Firefox-specific capabilities
*/
class FirefoxDriver implements WebDriver, JavascriptExecutor, TakesScreenshot, HasAuthentication, HasFullPageScreenshot, HasContext {
/**
* Create FirefoxDriver with default configuration
*/
FirefoxDriver();
/**
* Create FirefoxDriver with custom options
* @param options - FirefoxOptions for configuration
*/
FirefoxDriver(FirefoxOptions options);
/**
* Create FirefoxDriver with custom service
* @param service - GeckoDriverService for driver management
*/
FirefoxDriver(GeckoDriverService service);
/**
* Create FirefoxDriver with service and options
* @param service - GeckoDriverService for driver management
* @param options - FirefoxOptions for configuration
*/
FirefoxDriver(GeckoDriverService service, FirefoxOptions options);
}Configuration options for Firefox browser including arguments, profile, and preferences.
/**
* FirefoxOptions for configuring Firefox browser behavior
* Extends MutableCapabilities with Firefox-specific options
*/
class FirefoxOptions extends MutableCapabilities {
/**
* Create FirefoxOptions with default settings
*/
FirefoxOptions();
/**
* Add Firefox command line arguments
* @param arguments - Firefox arguments to add
* @return FirefoxOptions instance for chaining
*/
FirefoxOptions addArguments(String... arguments);
/**
* Add Firefox command line arguments from list
* @param arguments - List of Firefox arguments
* @return FirefoxOptions instance for chaining
*/
FirefoxOptions addArguments(List<String> arguments);
/**
* Set headless mode
* @param headless - true to enable headless mode
* @return FirefoxOptions instance for chaining
*/
FirefoxOptions setHeadless(boolean headless);
/**
* Set Firefox binary path
* @param path - Path to Firefox executable
* @return FirefoxOptions instance for chaining
*/
FirefoxOptions setBinary(String path);
/**
* Set Firefox binary file
* @param path - File pointing to Firefox executable
* @return FirefoxOptions instance for chaining
*/
FirefoxOptions setBinary(File path);
/**
* Set Firefox profile
* @param profile - FirefoxProfile for browser configuration
* @return FirefoxOptions instance for chaining
*/
FirefoxOptions setProfile(FirefoxProfile profile);
/**
* Set log level for browser logs
* @param logLevel - Log level (e.g., Level.INFO)
* @return FirefoxOptions instance for chaining
*/
FirefoxOptions setLogLevel(Level logLevel);
}WebDriver implementation for Microsoft Edge with Edge-specific capabilities.
/**
* EdgeDriver for Microsoft Edge browser automation
* Implements WebDriver with Edge-specific capabilities (Chromium-based)
*/
class EdgeDriver implements WebDriver, JavascriptExecutor, TakesScreenshot, HasAuthentication, HasBiDi, HasCasting, HasCdp, HasLaunching, HasLogEvents, HasNetworkConditions, HasPermissions, HasDownloads, HasDevTools {
/**
* Create EdgeDriver with default configuration
*/
EdgeDriver();
/**
* Create EdgeDriver with custom options
* @param options - EdgeOptions for configuration
*/
EdgeDriver(EdgeOptions options);
/**
* Create EdgeDriver with custom service
* @param service - EdgeDriverService for driver management
*/
EdgeDriver(EdgeDriverService service);
/**
* Create EdgeDriver with service and options
* @param service - EdgeDriverService for driver management
* @param options - EdgeOptions for configuration
*/
EdgeDriver(EdgeDriverService service, EdgeOptions options);
}WebDriver implementation for Apple Safari with Safari-specific capabilities.
/**
* SafariDriver for Apple Safari browser automation
* Implements WebDriver with Safari-specific capabilities
*/
class SafariDriver implements WebDriver, JavascriptExecutor, TakesScreenshot, HasAuthentication, HasPermissions, HasDebugger {
/**
* Create SafariDriver with default configuration
*/
SafariDriver();
/**
* Create SafariDriver with custom options
* @param options - SafariOptions for configuration
*/
SafariDriver(SafariOptions options);
/**
* Create SafariDriver with custom service
* @param service - SafariDriverService for driver management
*/
SafariDriver(SafariDriverService service);
/**
* Create SafariDriver with service and options
* @param service - SafariDriverService for driver management
* @param options - SafariOptions for configuration
*/
SafariDriver(SafariDriverService service, SafariOptions options);
}import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.chrome.ChromeDriverService;
import java.io.File;
// Basic Chrome setup
WebDriver driver = new ChromeDriver();
// Chrome with custom options
ChromeOptions options = new ChromeOptions();
options.addArguments("--headless"); // Run in headless mode
options.addArguments("--no-sandbox");
options.addArguments("--disable-dev-shm-usage");
options.addArguments("--window-size=1920,1080");
options.addArguments("--disable-gpu");
options.addArguments("--disable-extensions");
WebDriver headlessDriver = new ChromeDriver(options);
// Chrome with extensions
ChromeOptions extOptions = new ChromeOptions();
extOptions.addExtensions(new File("path/to/extension.crx"));
extOptions.addExtensions(new File("path/to/another-extension.crx"));
WebDriver driverWithExtensions = new ChromeDriver(extOptions);
// Chrome with experimental options
ChromeOptions expOptions = new ChromeOptions();
expOptions.setExperimentalOption("useAutomationExtension", false);
expOptions.setExperimentalOption("excludeSwitches", Arrays.asList("enable-automation"));
expOptions.setExperimentalOption("detach", true);
// Chrome with custom binary
expOptions.setBinary("/path/to/custom/chrome");
WebDriver customChromeDriver = new ChromeDriver(expOptions);// Mobile emulation
ChromeOptions mobileOptions = new ChromeOptions();
Map<String, Object> mobileEmulation = new HashMap<>();
mobileEmulation.put("deviceName", "iPhone X");
mobileOptions.setExperimentalOption("mobileEmulation", mobileEmulation);
// Custom mobile metrics
Map<String, Object> deviceMetrics = new HashMap<>();
deviceMetrics.put("width", 375);
deviceMetrics.put("height", 812);
deviceMetrics.put("pixelRatio", 3.0);
mobileEmulation.put("deviceMetrics", deviceMetrics);
mobileEmulation.put("userAgent", "Mozilla/5.0 (iPhone; CPU iPhone OS 11_0 like Mac OS X) AppleWebKit/604.1.38");
mobileOptions.setExperimentalOption("mobileEmulation", mobileEmulation);
WebDriver mobileDriver = new ChromeDriver(mobileOptions);
// Performance logging
ChromeOptions perfOptions = new ChromeOptions();
LoggingPreferences logPrefs = new LoggingPreferences();
logPrefs.enable(LogType.PERFORMANCE, Level.INFO);
perfOptions.setCapability(CapabilityType.LOGGING_PREFS, logPrefs);
WebDriver perfDriver = new ChromeDriver(perfOptions);import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.firefox.FirefoxOptions;
import org.openqa.selenium.firefox.FirefoxProfile;
import org.openqa.selenium.firefox.GeckoDriverService;
// Basic Firefox setup
WebDriver firefoxDriver = new FirefoxDriver();
// Firefox with options
FirefoxOptions firefoxOptions = new FirefoxOptions();
firefoxOptions.setHeadless(true);
firefoxOptions.addArguments("--width=1920");
firefoxOptions.addArguments("--height=1080");
WebDriver headlessFirefox = new FirefoxDriver(firefoxOptions);
// Firefox with custom profile
FirefoxProfile profile = new FirefoxProfile();
profile.setPreference("browser.download.folderList", 2);
profile.setPreference("browser.download.dir", "/path/to/downloads");
profile.setPreference("browser.helperApps.neverAsk.saveToDisk", "application/pdf,text/csv");
profile.setPreference("pdfjs.disabled", true);
FirefoxOptions profileOptions = new FirefoxOptions();
profileOptions.setProfile(profile);
WebDriver profileFirefox = new FirefoxDriver(profileOptions);
// Firefox with custom binary
firefoxOptions.setBinary("/path/to/firefox");import org.openqa.selenium.edge.EdgeDriver;
import org.openqa.selenium.edge.EdgeOptions;
// Basic Edge setup
WebDriver edgeDriver = new EdgeDriver();
// Edge with options (similar to Chrome since it's Chromium-based)
EdgeOptions edgeOptions = new EdgeOptions();
edgeOptions.addArguments("--headless");
edgeOptions.addArguments("--disable-gpu");
edgeOptions.addArguments("--window-size=1920,1080");
WebDriver headlessEdge = new EdgeDriver(edgeOptions);
// Edge with experimental options
edgeOptions.setExperimentalOption("useAutomationExtension", false);
edgeOptions.setExperimentalOption("excludeSwitches", Arrays.asList("enable-automation"));import org.openqa.selenium.safari.SafariDriver;
import org.openqa.selenium.safari.SafariOptions;
// Basic Safari setup (macOS only)
WebDriver safariDriver = new SafariDriver();
// Safari with options
SafariOptions safariOptions = new SafariOptions();
safariOptions.setUseTechnologyPreview(true); // Use Safari Technology Preview
safariOptions.setAutomaticInspection(false);
safariOptions.setAutomaticProfiling(false);
WebDriver tpSafari = new SafariDriver(safariOptions);import org.openqa.selenium.chrome.ChromeDriverService;
import org.openqa.selenium.firefox.GeckoDriverService;
import java.io.File;
// Chrome service configuration
ChromeDriverService chromeService = new ChromeDriverService.Builder()
.usingDriverExecutable(new File("/path/to/chromedriver"))
.usingAnyFreePort()
.withEnvironment(Map.of("DISPLAY", ":0"))
.withLogLevel(ChromeDriverService.LOG_LEVEL_ALL)
.withSilent(false)
.withVerbose(true)
.build();
ChromeOptions chromeOptions = new ChromeOptions();
WebDriver chromeDriverWithService = new ChromeDriver(chromeService, chromeOptions);
// Firefox service configuration
GeckoDriverService geckoService = new GeckoDriverService.Builder()
.usingDriverExecutable(new File("/path/to/geckodriver"))
.usingAnyFreePort()
.withLogLevel(Level.INFO)
.build();
FirefoxOptions firefoxOptions = new FirefoxOptions();
WebDriver firefoxDriverWithService = new FirefoxDriver(geckoService, firefoxOptions);public WebDriver createDriver(String browserName) {
WebDriver driver;
switch (browserName.toLowerCase()) {
case "chrome":
ChromeOptions chromeOptions = new ChromeOptions();
chromeOptions.addArguments("--disable-blink-features=AutomationControlled");
chromeOptions.setExperimentalOption("useAutomationExtension", false);
chromeOptions.setExperimentalOption("excludeSwitches", Arrays.asList("enable-automation"));
driver = new ChromeDriver(chromeOptions);
break;
case "firefox":
FirefoxOptions firefoxOptions = new FirefoxOptions();
firefoxOptions.addArguments("--width=1920");
firefoxOptions.addArguments("--height=1080");
driver = new FirefoxDriver(firefoxOptions);
break;
case "edge":
EdgeOptions edgeOptions = new EdgeOptions();
edgeOptions.addArguments("--disable-blink-features=AutomationControlled");
driver = new EdgeDriver(edgeOptions);
break;
case "safari":
SafariOptions safariOptions = new SafariOptions();
driver = new SafariDriver(safariOptions);
break;
default:
throw new IllegalArgumentException("Browser not supported: " + browserName);
}
// Common configuration
driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(10));
driver.manage().window().maximize();
return driver;
}
// Usage
WebDriver driver = createDriver("chrome");// Chrome with custom user data directory
ChromeOptions advancedOptions = new ChromeOptions();
advancedOptions.addArguments("--user-data-dir=/path/to/user/data");
advancedOptions.addArguments("--profile-directory=Profile 1");
// Disable images and CSS for faster loading
advancedOptions.addArguments("--blink-settings=imagesEnabled=false");
advancedOptions.setExperimentalOption("prefs", Map.of(
"profile.managed_default_content_settings.images", 2,
"profile.default_content_setting_values.stylesheet", 2
));
// Chrome with proxy
Proxy proxy = new Proxy();
proxy.setHttpProxy("proxy.company.com:8080");
proxy.setSslProxy("proxy.company.com:8080");
advancedOptions.setCapability(CapabilityType.PROXY, proxy);
// Chrome with download preferences
Map<String, Object> prefs = new HashMap<>();
prefs.put("download.default_directory", "/path/to/downloads");
prefs.put("download.prompt_for_download", false);
prefs.put("plugins.always_open_pdf_externally", true);
advancedOptions.setExperimentalOption("prefs", prefs);
WebDriver advancedDriver = new ChromeDriver(advancedOptions);// Proper driver cleanup
public void cleanupDriver(WebDriver driver) {
if (driver != null) {
try {
// Close all windows
driver.quit();
} catch (Exception e) {
System.err.println("Error during driver cleanup: " + e.getMessage());
}
}
}
// Try-with-resources pattern (if driver implements AutoCloseable)
public void useDriverSafely() {
ChromeOptions options = new ChromeOptions();
options.setHeadless(true);
try (ChromeDriver driver = new ChromeDriver(options)) {
driver.get("https://example.com");
// Perform automation tasks
} catch (Exception e) {
System.err.println("Error during automation: " + e.getMessage());
}
// Driver automatically closed
}
// Shutdown hook for cleanup
Runtime.getRuntime().addShutdownHook(new Thread(() -> {
if (driver != null) {
driver.quit();
}
}));Install with Tessl CLI
npx tessl i tessl/maven-org-seleniumhq-selenium--selenium-api