DesiredCapabilities in Selenium
DesiredCapabilities in Selenium Interview with follow-up questions
1. What is DesiredCapabilities in Selenium?
DesiredCapabilities was a Selenium class for specifying browser configuration—browser name, version, platform, and additional settings—when creating a WebDriver instance or connecting to a Grid.
Current status in Selenium 4
DesiredCapabilities is largely deprecated for local browser instantiation. Selenium 4 uses browser-specific Options classes instead:
ChromeOptionsfor ChromeFirefoxOptionsfor FirefoxEdgeOptionsfor Microsoft EdgeSafariOptionsfor Safari
These Options classes extend MutableCapabilities (which itself extends DesiredCapabilities internally), so they carry all the same capability-setting ability but with a type-safe, browser-specific API.
Why the shift
The W3C WebDriver standard (fully adopted in Selenium 4) defines a strict capability structure. Browser-specific capabilities must be placed in a vendor namespace (e.g., goog:chromeOptions, moz:firefoxOptions). The old DesiredCapabilities API did not enforce this structure, leading to capabilities being silently ignored by W3C-compliant drivers. Options classes handle the namespacing automatically.
Where DesiredCapabilities still appears
- In Selenium Grid configurations, when passing capabilities to
RemoteWebDriver, Options objects are used directly (they implement theCapabilitiesinterface). - Legacy codebases that have not yet migrated.
- Some third-party cloud testing platforms still document
DesiredCapabilitiesin older tutorials.
Summary for interviews
Mention that you know DesiredCapabilities is the older API, that Selenium 4 favors browser-specific Options classes, and that you use ChromeOptions or FirefoxOptions in current projects. Show awareness of why the change was made (W3C compliance, type safety).
Follow-up 1
How do you use DesiredCapabilities?
To use DesiredCapabilities in Selenium, you need to create an instance of the DesiredCapabilities class and set the desired properties using its methods. For example, to set the browser name to Chrome and the browser version to 91.0, you can use the following code:
from selenium import webdriver
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
# Create an instance of DesiredCapabilities
capabilities = DesiredCapabilities.CHROME
# Set the desired properties
capabilities['browserName'] = 'chrome'
capabilities['browserVersion'] = '91.0'
# Create a WebDriver instance with the desired capabilities
driver = webdriver.Remote(command_executor='http://localhost:4444/wd/hub', desired_capabilities=capabilities)
Follow-up 2
What are some common use cases for DesiredCapabilities?
Some common use cases for DesiredCapabilities in Selenium include:
Specifying the browser and its version: DesiredCapabilities can be used to specify the browser name and version to be used for testing. This is useful when you want to test your application on different browser versions.
Setting the platform: DesiredCapabilities can be used to set the platform on which the tests will be executed. This is useful when you want to test your application on different operating systems.
Configuring additional settings: DesiredCapabilities can be used to configure additional settings for the WebDriver instance, such as enabling/disabling JavaScript, accepting SSL certificates, setting proxy configurations, etc.
Follow-up 3
Can you provide an example of a scenario where DesiredCapabilities would be useful?
Sure! Let's say you have a web application that needs to be tested on different browsers and operating systems. You can use DesiredCapabilities to specify the desired browser, version, and platform for each test scenario. For example, you can create separate test cases for Chrome on Windows, Firefox on macOS, and Safari on iOS. By using DesiredCapabilities, you can easily switch between different browser configurations without modifying your test code.
Here's an example code snippet that demonstrates how to use DesiredCapabilities to test a web application on different browsers:
from selenium import webdriver
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
# Create an instance of DesiredCapabilities for Chrome
chrome_capabilities = DesiredCapabilities.CHROME
chrome_capabilities['browserName'] = 'chrome'
chrome_capabilities['browserVersion'] = '91.0'
chrome_capabilities['platform'] = 'WINDOWS'
# Create an instance of DesiredCapabilities for Firefox
firefox_capabilities = DesiredCapabilities.FIREFOX
firefox_capabilities['browserName'] = 'firefox'
firefox_capabilities['browserVersion'] = '89.0'
firefox_capabilities['platform'] = 'MAC'
# Create WebDriver instances with the desired capabilities
chrome_driver = webdriver.Remote(command_executor='http://localhost:4444/wd/hub', desired_capabilities=chrome_capabilities)
firefox_driver = webdriver.Remote(command_executor='http://localhost:4444/wd/hub', desired_capabilities=firefox_capabilities)
# Perform your tests using the WebDriver instances
# ...
# Close the WebDriver instances
chrome_driver.quit()
firefox_driver.quit()
2. How can DesiredCapabilities be used to set browser properties?
In Selenium 4, browser properties are set using browser-specific Options classes rather than the deprecated DesiredCapabilities API.
ChromeOptions
ChromeOptions options = new ChromeOptions();
options.addArguments("--headless=new"); // headless mode (updated flag)
options.addArguments("--no-sandbox"); // required in some CI environments
options.addArguments("--disable-dev-shm-usage"); // overcome limited /dev/shm in Docker
options.addArguments("--window-size=1920,1080");
options.addArguments("--incognito");
options.addExtensions(new File("path/to/extension.crx"));
options.setAcceptInsecureCerts(true);
WebDriver driver = new ChromeDriver(options);
FirefoxOptions
FirefoxOptions options = new FirefoxOptions();
options.addArguments("-headless");
options.addPreference("network.proxy.type", 1);
options.setAcceptInsecureCerts(true);
WebDriver driver = new FirefoxDriver(options);
EdgeOptions
EdgeOptions options = new EdgeOptions();
options.addArguments("--inprivate");
WebDriver driver = new EdgeDriver(options);
For RemoteWebDriver (Selenium Grid)
ChromeOptions options = new ChromeOptions();
options.addArguments("--headless=new");
WebDriver driver = new RemoteWebDriver(new URL("http://localhost:4444"), options);
Options objects implement Capabilities, so they are passed directly to RemoteWebDriver wherever DesiredCapabilities was used before.
Setting raw W3C capabilities Options also support setting capabilities that do not have a dedicated method:
options.setCapability("browserVersion", "stable");
options.setCapability("platformName", "linux");
Using Options is the correct modern approach—it provides type safety, IDE autocomplete, and correct W3C capability namespacing.
Follow-up 1
What happens if you try to set a browser property that doesn't exist using DesiredCapabilities?
If you try to set a browser property that doesn't exist using DesiredCapabilities, it will not have any effect on the browser. The browser will be launched with its default properties or the properties specified by the WebDriver implementation. It is important to note that the available properties and their names may vary depending on the browser and WebDriver implementation you are using. It is recommended to refer to the documentation or official resources for the specific browser and WebDriver version you are working with to ensure the correct usage of DesiredCapabilities.
Follow-up 2
Can you provide an example of setting a browser property using DesiredCapabilities?
Sure! Here is an example of setting the browser name property using DesiredCapabilities in Java:
DesiredCapabilities capabilities = new DesiredCapabilities();
capabilities.setBrowserName("chrome");
WebDriver driver = new ChromeDriver(capabilities);
In this example, we create a new instance of DesiredCapabilities and set the browser name property to "chrome". We then pass the DesiredCapabilities object to the ChromeDriver constructor to launch the Chrome browser with the specified property.
Follow-up 3
What are some common browser properties that can be set using DesiredCapabilities?
Some common browser properties that can be set using DesiredCapabilities include:
- Browser name: You can set the browser name property to specify the browser to be used, such as "chrome", "firefox", or "safari".
- Browser version: You can set the browser version property to specify a specific version of the browser.
- Platform: You can set the platform property to specify the operating system on which the browser should be launched, such as "Windows", "Mac", or "Linux".
- Proxy settings: You can set the proxy property to configure the browser to use a proxy server.
These are just a few examples, and there are many other properties that can be set using DesiredCapabilities depending on the browser and WebDriver implementation you are using.
3. What is the difference between DesiredCapabilities and WebDriver Options?
DesiredCapabilities and browser-specific Options classes both configure WebDriver sessions, but they differ in design and current relevance.
DesiredCapabilities (older API)
- Generic: a
Mapof capability key-value pairs. - No type safety—invalid keys are silently accepted and may be ignored by the driver.
- Does not automatically apply browser-specific capability namespacing required by W3C WebDriver.
- Example:
caps.setBrowserName("chrome"); caps.setVersion("114"); - Status: deprecated for new development in Selenium 4.
Browser Options classes (current API)
- Browser-specific:
ChromeOptions,FirefoxOptions,EdgeOptions,SafariOptions. - Type-safe methods with IDE autocomplete:
options.addArguments(...),options.setAcceptInsecureCerts(true). - Automatically places browser-specific settings in the correct W3C vendor namespace (e.g.,
goog:chromeOptions), which W3C-compliant drivers require. - Extend
MutableCapabilitieswhich extendsDesiredCapabilitiesinternally, so they implement theCapabilitiesinterface and work anywhere that previously acceptedDesiredCapabilities. - Example:
ChromeOptions options = new ChromeOptions(); options.addArguments("--headless=new");
Practical impact
With the old API, passing a DesiredCapabilities object to ChromeDriver 115+ would cause browser-specific options to be dropped because they were not in the goog:chromeOptions namespace. ChromeOptions handles this namespacing automatically.
When DesiredCapabilities still appears Reading legacy code, working with older Selenium Grid documentation, or integrating with cloud provider platforms that still reference the old API in their setup guides.
Interview summary: Prefer Options classes in all new code. Know that they are backward-compatible replacements for DesiredCapabilities and explain why the change was made (W3C compliance and type safety).
Follow-up 1
In what scenarios would you use DesiredCapabilities over WebDriver Options?
DesiredCapabilities are typically used when you need to specify the browser and platform configurations for the WebDriver. For example, if you want to run your tests on a specific version of Chrome on Windows, you can use DesiredCapabilities to set the browser name to 'chrome', the browser version to the desired version, and the platform to 'WINDOWS'.
DesiredCapabilities are also useful when you need to set additional capabilities that are not available through WebDriver Options. For example, if you want to enable JavaScript execution in the browser, you can use DesiredCapabilities to set the 'javascriptEnabled' capability to 'true'.
In summary, you would use DesiredCapabilities over WebDriver Options when you need to specify the browser and platform configurations or set additional capabilities that are not available through WebDriver Options.
Follow-up 2
Can WebDriver Options perform all the functions of DesiredCapabilities?
No, WebDriver Options cannot perform all the functions of DesiredCapabilities. While WebDriver Options provide a more fine-grained control over the WebDriver's behavior, they do not allow you to set the browser and platform configurations or set additional capabilities that are not available through WebDriver Options.
DesiredCapabilities are specifically designed for setting the desired capabilities of the WebDriver, including the browser name, version, platform, and additional capabilities.
In summary, while WebDriver Options provide more control over the WebDriver's behavior, they cannot perform all the functions of DesiredCapabilities.
Follow-up 3
Can you provide an example of a scenario where WebDriver Options would be more appropriate to use than DesiredCapabilities?
Certainly! One scenario where WebDriver Options would be more appropriate to use than DesiredCapabilities is when you want to set the browser's window size or maximize the window.
Here's an example using Python:
from selenium import webdriver
options = webdriver.ChromeOptions()
options.add_argument('--window-size=1280,720')
driver = webdriver.Chrome(options=options)
4. How can DesiredCapabilities be used in Selenium Grid?
With Selenium Grid 4, the recommended approach is to pass browser Options objects directly to RemoteWebDriver rather than using DesiredCapabilities directly.
Connecting to Grid with ChromeOptions
ChromeOptions options = new ChromeOptions();
options.addArguments("--headless=new");
options.setAcceptInsecureCerts(true);
WebDriver driver = new RemoteWebDriver(
new URL("http://localhost:4444"),
options
);
Options implement the Capabilities interface, so RemoteWebDriver accepts them directly.
Specifying platform and browser version for Grid routing
ChromeOptions options = new ChromeOptions();
options.setCapability("browserVersion", "stable");
options.setCapability("platformName", "linux");
Grid 4 uses these capabilities to route the session to a matching node.
Legacy pattern to avoid
// Old approach — do not use in new code
DesiredCapabilities caps = new DesiredCapabilities();
caps.setBrowserName("chrome");
caps.setCapability(ChromeOptions.CAPABILITY, chromeOptions);
new RemoteWebDriver(gridUrl, caps);
This pattern is unnecessary in Selenium 4 because ChromeOptions already carries all the capability information directly.
Grid 4 UI verification
After creating a session, check http://localhost:4444/ui to confirm the session is running on the expected node with the expected browser.
Cloud Grid providers (BrowserStack, Sauce Labs, LambdaTest)
These platforms accept Options objects with additional vendor-specific capabilities set via setCapability(). Each provider's documentation specifies the required capability keys and values for their platform, replacing what was previously done with raw DesiredCapabilities.
Follow-up 1
Can you provide an example of using DesiredCapabilities in Selenium Grid?
Sure! Here's an example of using DesiredCapabilities in Selenium Grid:
from selenium import webdriver
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
# Define the desired capabilities
capabilities = DesiredCapabilities.CHROME.copy()
capabilities['platform'] = 'WINDOWS'
capabilities['version'] = 'latest'
# Create a remote WebDriver instance
driver = webdriver.Remote(
command_executor='http://:/wd/hub',
desired_capabilities=capabilities
)
# Use the driver for your tests
# ...
# Quit the driver
driver.quit()
In this example, we are using the Chrome browser on a Windows platform with the latest version. You need to replace and with the actual IP address and port of your Selenium Grid hub.
Follow-up 2
What are some common use cases for DesiredCapabilities in Selenium Grid?
Some common use cases for DesiredCapabilities in Selenium Grid include:
- Running tests on different browsers (e.g., Chrome, Firefox, Safari) and platforms (e.g., Windows, macOS, Linux) simultaneously
- Testing against different browser versions to ensure compatibility
- Running tests in parallel on multiple remote WebDriver nodes
- Distributing test execution across multiple machines for faster execution
- Testing against different browser configurations (e.g., headless mode, specific browser settings)
DesiredCapabilities provide flexibility and control over the WebDriver instances used in Selenium Grid, allowing you to tailor your test execution environment to your specific requirements.
Follow-up 3
How does DesiredCapabilities enhance the functionality of Selenium Grid?
DesiredCapabilities enhance the functionality of Selenium Grid by allowing you to customize and configure the WebDriver instances used in the grid. By specifying the desired capabilities, you can control the browser, platform, version, and other settings for the remote WebDriver nodes. This enables you to run tests on different browsers and platforms simultaneously, distribute test execution across multiple machines, and test against various browser configurations. DesiredCapabilities provide the flexibility and scalability needed to effectively utilize the resources of a Selenium Grid and maximize test coverage.
5. Can DesiredCapabilities handle SSL certificates in Selenium?
Handling SSL certificate errors is a common requirement in test environments that use self-signed certificates. In Selenium 4, this is done through the browser Options classes rather than DesiredCapabilities.
Selenium 4 approach (current)
// Chrome
ChromeOptions options = new ChromeOptions();
options.setAcceptInsecureCerts(true);
WebDriver driver = new ChromeDriver(options);
// Firefox
FirefoxOptions options = new FirefoxOptions();
options.setAcceptInsecureCerts(true);
WebDriver driver = new FirefoxDriver(options);
// Edge
EdgeOptions options = new EdgeOptions();
options.setAcceptInsecureCerts(true);
WebDriver driver = new EdgeDriver(options);
setAcceptInsecureCerts(true) is a standard W3C WebDriver capability. It instructs the browser to bypass TLS/SSL certificate errors for self-signed, expired, or otherwise invalid certificates during the test session.
For RemoteWebDriver (Grid)
ChromeOptions options = new ChromeOptions();
options.setAcceptInsecureCerts(true);
WebDriver driver = new RemoteWebDriver(new URL("http://localhost:4444"), options);
Old DesiredCapabilities approach (deprecated)
// Do not use in new code
DesiredCapabilities caps = new DesiredCapabilities();
caps.setCapability(CapabilityType.ACCEPT_SSL_CERTS, true);
caps.setCapability(CapabilityType.ACCEPT_INSECURE_CERTS, true);
This still works in some configurations for backward compatibility, but setAcceptInsecureCerts() on the Options object is the correct Selenium 4 approach.
Important security note
setAcceptInsecureCerts(true) is intended for test environments only. Never use it against production systems—bypassing certificate validation defeats TLS security and could expose the test to man-in-the-middle attacks.
Follow-up 1
How does DesiredCapabilities handle SSL certificates?
DesiredCapabilities handles SSL certificates by providing a capability called 'acceptSslCerts'. By setting this capability to 'true', the browser will accept SSL certificates without displaying any warnings or errors.
Follow-up 2
Can you provide an example of handling SSL certificates using DesiredCapabilities?
Certainly! Here's an example of how to handle SSL certificates using DesiredCapabilities in Selenium:
from selenium import webdriver
# Create a DesiredCapabilities object
capabilities = webdriver.DesiredCapabilities.CHROME.copy()
# Set the 'acceptSslCerts' capability to True
capabilities['acceptSslCerts'] = True
# Create a WebDriver instance with the desired capabilities
driver = webdriver.Chrome(desired_capabilities=capabilities)
# Your code here...
In this example, we create a DesiredCapabilities object and set the 'acceptSslCerts' capability to True. Then, we pass the desired capabilities to the Chrome WebDriver constructor to create a WebDriver instance that can handle SSL certificates.
Follow-up 3
What are some challenges of handling SSL certificates in Selenium and how does DesiredCapabilities address these challenges?
Handling SSL certificates in Selenium can be challenging because browsers often display warnings or errors when encountering untrusted or expired certificates. This can disrupt the automation process and require manual intervention.
DesiredCapabilities addresses these challenges by allowing you to set the 'acceptSslCerts' capability to True. By enabling this capability, the browser will automatically accept SSL certificates without displaying any warnings or errors, allowing the automation to proceed smoothly.
Live mock interview
Mock interview: DesiredCapabilities in Selenium
- Read your scene and goals
- Talk it out; goals tick off live
- Get a score and stronger lines
Your voice and your AI key never touch our servers; the key stays in this browser and is sent only to Google. Only your round scores are saved to track progress.