Answer
DesiredCapabilities in Selenium
DesiredCapabilities is a class in Selenium used to set the browser attributes and configuration before launching a browser session via WebDriver. It tells Selenium which browser, version, OS, and features to use.
Common Use Cases
- ✓Setting browser name and version
- ✓Configuring Selenium Grid (Hub + Node)
- ✓Enabling/disabling JavaScript, SSL certificates
- ✓Setting proxy, platform, and mobile emulation
Example — ChromeDriver with Capabilities
Java
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
ChromeOptions options = new ChromeOptions();
options.addArguments("--disable-notifications");
options.addArguments("--incognito");
options.setAcceptInsecureCerts(true); // accept SSL errors
WebDriver driver = new ChromeDriver(options);
driver.get("https://example.com");
Example — Selenium Grid (RemoteWebDriver)
Java
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.remote.RemoteWebDriver;
import java.net.URL;
DesiredCapabilities caps = new DesiredCapabilities();
caps.setBrowserName("chrome");
caps.setPlatform(Platform.LINUX);
caps.setVersion("latest");
WebDriver driver = new RemoteWebDriver(
new URL("http://localhost:4444/wd/hub"), caps
);
Key Methods
| Method | Description |
|---|---|
setBrowserName("chrome") | Set which browser to use |
setPlatform(Platform.WIN10) | Set the OS platform |
setVersion("latest") | Set browser version |
setJavascriptEnabled(true) | Enable/disable JS |
setAcceptInsecureCerts(true) | Accept SSL errors |
setCapability("key", value) | Set any custom capability |
Note (Selenium 4)
In Selenium 4, DesiredCapabilities is largely replaced by browser-specific Options classes (ChromeOptions, FirefoxOptions, etc.), which provide stronger type safety and better IDE support.
