Selenium Chrome WebDriver implementation for automating Chrome browsers using the WebDriver protocol
—
Chrome-specific browser configuration including binary paths, command-line arguments, extensions, experimental options, and capability settings for customizing Chrome browser behavior during automated testing.
Configuration options for Chrome browser instances providing extensive customization for headless operation, extension management, proxy settings, and browser arguments.
/**
* Class to manage options specific to ChromeDriver.
* Extends MutableCapabilities to provide Chrome-specific configuration.
*/
public class ChromeOptions extends MutableCapabilities {
/**
* Key used to store a set of ChromeOptions in a Capabilities object.
*/
public static final String CAPABILITY = "goog:chromeOptions";
/**
* Creates a new ChromeOptions instance with default Chrome capabilities.
*/
public ChromeOptions();
/**
* Merges additional capabilities into these ChromeOptions.
* @param extraCapabilities Additional capabilities to merge
* @return This ChromeOptions instance for method chaining
*/
public ChromeOptions merge(Capabilities extraCapabilities);
}Configure the Chrome executable location for custom Chrome installations.
/**
* Sets the path to the Chrome executable from File.
* @param path Path to Chrome executable file
* @return This ChromeOptions instance for method chaining
*/
public ChromeOptions setBinary(File path);
/**
* Sets the path to the Chrome executable from String.
* @param path Path to Chrome executable as string
* @return This ChromeOptions instance for method chaining
*/
public ChromeOptions setBinary(String path);Usage Example:
ChromeOptions options = new ChromeOptions();
options.setBinary("/usr/bin/google-chrome-stable");
// or
options.setBinary(new File("/Applications/Google Chrome.app/Contents/MacOS/Google Chrome"));Add Chrome command-line arguments for browser behavior customization.
/**
* Adds additional command line arguments (varargs).
* @param arguments The arguments to use when starting Chrome
* @return This ChromeOptions instance for method chaining
*/
public ChromeOptions addArguments(String... arguments);
/**
* Adds additional command line arguments from list.
* Each argument may contain an option "--" prefix: "--foo" or "foo".
* Arguments with an associated value should be delimited with an "=": "foo=bar".
* @param arguments The arguments to use when starting Chrome
* @return This ChromeOptions instance for method chaining
*/
public ChromeOptions addArguments(List<String> arguments);Usage Examples:
ChromeOptions options = new ChromeOptions();
// Common arguments for headless testing
options.addArguments(
"--headless",
"--no-sandbox",
"--disable-dev-shm-usage",
"--disable-gpu",
"--window-size=1920,1080"
);
// Performance and debugging arguments
options.addArguments(
"--disable-extensions",
"--disable-plugins",
"--disable-images",
"--disable-javascript",
"--remote-debugging-port=9222"
);
// User data and profile arguments
options.addArguments(
"--user-data-dir=/tmp/chrome-test-profile",
"--profile-directory=Default"
);Install Chrome extensions from files or encoded data.
/**
* Adds Chrome extensions from files (varargs).
* @param paths Paths to the extensions (.crx files) to install
* @return This ChromeOptions instance for method chaining
*/
public ChromeOptions addExtensions(File... paths);
/**
* Adds Chrome extensions from file list.
* Each path should specify a packed Chrome extension (CRX file).
* @param paths Paths to the extensions to install
* @return This ChromeOptions instance for method chaining
*/
public ChromeOptions addExtensions(List<File> paths);
/**
* Adds Base64 encoded extensions (varargs).
* @param encoded Base64 encoded data of the extensions to install
* @return This ChromeOptions instance for method chaining
*/
public ChromeOptions addEncodedExtensions(String... encoded);
/**
* Adds Base64 encoded extensions from list.
* Each string should specify a Base64 encoded packed Chrome extension (CRX file).
* @param encoded Base64 encoded data of the extensions to install
* @return This ChromeOptions instance for method chaining
*/
public ChromeOptions addEncodedExtensions(List<String> encoded);Usage Examples:
ChromeOptions options = new ChromeOptions();
// Add extensions from files
options.addExtensions(
new File("/path/to/extension1.crx"),
new File("/path/to/extension2.crx")
);
// Add extensions from Base64 encoded strings
String encodedExtension = "UEsDBBQAAAAIAA..."; // Base64 encoded CRX
options.addEncodedExtensions(encodedExtension);
// Add multiple extensions from list
List<File> extensions = Arrays.asList(
new File("/path/to/adblock.crx"),
new File("/path/to/dev-tools.crx")
);
options.addExtensions(extensions);Configure experimental Chrome features and advanced settings.
/**
* Sets an experimental option. Useful for new ChromeDriver options not yet
* exposed through the ChromeOptions API.
* @param name Name of the experimental option
* @param value Value of the experimental option, which must be convertible to JSON
* @return This ChromeOptions instance for method chaining
*/
public ChromeOptions setExperimentalOption(String name, Object value);
/**
* Returns the value of an experimental option.
* @param name The option name
* @return The option value, or null if not set
* @deprecated Getters are not needed in browser Options classes
*/
@Deprecated
public Object getExperimentalOption(String name);Usage Examples:
ChromeOptions options = new ChromeOptions();
// Mobile emulation
Map<String, Object> mobileEmulation = new HashMap<>();
mobileEmulation.put("deviceName", "iPhone X");
options.setExperimentalOption("mobileEmulation", mobileEmulation);
// Performance logging
Map<String, Object> perfLoggingPrefs = new HashMap<>();
perfLoggingPrefs.put("enableNetwork", true);
perfLoggingPrefs.put("enablePage", true);
options.setExperimentalOption("perfLoggingPrefs", perfLoggingPrefs);
// Chrome preferences
Map<String, Object> prefs = new HashMap<>();
prefs.put("download.default_directory", "/path/to/downloads");
prefs.put("profile.default_content_settings.popups", 0);
options.setExperimentalOption("prefs", prefs);Configure how ChromeDriver waits for page loads to complete.
/**
* Sets the page load strategy for the browser session.
* @param strategy The page load strategy to use (NORMAL, EAGER, NONE)
* @return This ChromeOptions instance for method chaining
*/
public ChromeOptions setPageLoadStrategy(PageLoadStrategy strategy);Configure how ChromeDriver handles unexpected alert dialogs.
/**
* Sets the unhandled prompt behavior for alert dialogs.
* @param behaviour How to handle unexpected alert dialogs (ACCEPT, DISMISS, IGNORE)
* @return This ChromeOptions instance for method chaining
*/
public ChromeOptions setUnhandledPromptBehaviour(UnexpectedAlertBehaviour behaviour);Configure certificate and security-related browser settings.
/**
* Sets whether to accept insecure certificates.
* @param acceptInsecureCerts True to accept insecure certificates, false otherwise
* @return This ChromeOptions instance for method chaining
*/
public ChromeOptions setAcceptInsecureCerts(boolean acceptInsecureCerts);Enable or disable headless browser operation for server environments.
/**
* Enables or disables headless mode. In headless mode, Chrome runs without a GUI.
* @param headless True to enable headless mode, false to disable
* @return This ChromeOptions instance for method chaining
*/
public ChromeOptions setHeadless(boolean headless);Usage Example:
ChromeOptions options = new ChromeOptions();
options.setHeadless(true); // Enables headless mode and adds --disable-gpuConfigure proxy settings for network traffic routing.
/**
* Sets the proxy configuration for the browser session.
* @param proxy The proxy configuration to use
* @return This ChromeOptions instance for method chaining
*/
public ChromeOptions setProxy(Proxy proxy);Usage Example:
import org.openqa.selenium.Proxy;
ChromeOptions options = new ChromeOptions();
Proxy proxy = new Proxy();
proxy.setHttpProxy("proxy.company.com:8080");
proxy.setSslProxy("proxy.company.com:8080");
options.setProxy(proxy);Convert ChromeOptions to map representation for JSON serialization.
/**
* Converts the ChromeOptions to a map representation for JSON serialization.
* @return Unmodifiable map containing all option values
*/
public Map<String, Object> asMap();import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.PageLoadStrategy;
import org.openqa.selenium.UnexpectedAlertBehaviour;
import org.openqa.selenium.Proxy;
import java.util.HashMap;
import java.util.Map;
ChromeOptions options = new ChromeOptions();
// Basic configuration
options.setBinary("/usr/bin/google-chrome-stable");
options.setHeadless(true);
options.setPageLoadStrategy(PageLoadStrategy.EAGER);
options.setAcceptInsecureCerts(true);
options.setUnhandledPromptBehaviour(UnexpectedAlertBehaviour.DISMISS);
// Command line arguments
options.addArguments(
"--no-sandbox",
"--disable-dev-shm-usage",
"--disable-gpu",
"--window-size=1920,1080",
"--disable-extensions",
"--remote-debugging-port=9222"
);
// Extensions
options.addExtensions(new File("/path/to/extension.crx"));
// Experimental options
Map<String, Object> prefs = new HashMap<>();
prefs.put("download.default_directory", "/tmp/downloads");
prefs.put("profile.default_content_settings.popups", 0);
options.setExperimentalOption("prefs", prefs);
// Mobile emulation
Map<String, Object> mobileEmulation = new HashMap<>();
mobileEmulation.put("deviceName", "iPhone X");
options.setExperimentalOption("mobileEmulation", mobileEmulation);
// Create driver with configured options
ChromeDriver driver = new ChromeDriver(options);// Page load strategies
enum PageLoadStrategy {
/**
* Wait for the page to finish loading (default)
*/
NORMAL,
/**
* Wait for initial HTML document to be loaded
*/
EAGER,
/**
* Do not wait for page to load
*/
NONE
}
// Alert handling behaviors
enum UnexpectedAlertBehaviour {
/**
* Accept unexpected alert dialogs
*/
ACCEPT,
/**
* Dismiss unexpected alert dialogs
*/
DISMISS,
/**
* Ignore unexpected alert dialogs
*/
IGNORE
}Install with Tessl CLI
npx tessl i tessl/maven-org-seleniumhq-selenium--selenium-chrome-driver