What is the use of DesiredCapabilities in Selenium?

DesiredCapabilities is a class in Selenium WebDriver used to set properties for the browsers. These properties can be anything from the type of browser, version of the browser, path of the browser driver in the system, or setting up a browser profile.

When you are running a test case with a specific browser, Selenium needs to know the browser's properties. So, you set these properties using DesiredCapabilities. It is mostly used for setting up browser properties in Selenium Grid where you run your test case in a remote machine.

Here is an example of how you can use DesiredCapabilities in Selenium with Python:

from selenium import webdriver
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities

cap = DesiredCapabilities.CHROME
cap["marionette"] = False

driver = webdriver.Chrome(desired_capabilities=cap)
driver.get("https://www.google.com")

In this example, we are setting the marionette property to False for the Chrome browser. marionette is a property that if set to True, the WebDriver will use Marionette instead of ChromeDriver.

And here is an equivalent example in JavaScript (Node.js) using WebDriverJS:

const {Builder} = require('selenium-webdriver');
const chrome = require('selenium-webdriver/chrome');

let o = new chrome.Options();
o.addArguments('disable-infobars');
o.setUserPreferences({ credential_enable_service: false });

let driver = new Builder()
   .forBrowser('chrome')
   .setChromeOptions(o)
   .build();

driver.get('https://www.google.com');

In this JavaScript example, we are disabling the 'infobars' that Chrome shows by default, like the "Chrome is being controlled by automated test software" bar. Also, it is disabling the autofill service in Chrome.

Please note that DesiredCapabilities is being deprecated in Selenium. Starting from Selenium 4, it's recommended to use the new Options classes to customize browser capabilities. The Options classes are more browser-specific and provide a better API to manage browser capabilities. For example, you have ChromeOptions, FirefoxOptions, and InternetExplorerOptions for Chrome, Firefox, and IE respectively.

Related Questions

Get Started Now

WebScraping.AI provides rotating proxies, Chromium rendering and built-in HTML parser for web scraping
Icon