ChromeOptions in Selenium WebDriver
ChromeOptions is a new concept added in Selenium WebDriver starting from Selenium version 3.6.0 which is used for customizing the ChromeDriver session.
By default when selenium opens up any browser (Chrome browser or Firefox browser), it opens up without any extension or history or cookies, etc.
In the Chrome browser, you must have observed a ribbon (info-bar) coming at the top showing a message – “This chrome browser is controlled by Automation tool”.
Chrome Options can be used to alter these default settings and can disable info-bar messages etc.
Creating an instance of ChromeOptions class:
You can create an instance of ChromeOptions class which has methods to set specific chrome driver-related configurations. Then this instance passed in the constructor of ChromeDriver. See below code:
ChromeOptions options = new ChromeOptions(); options.addArguments("disable-infobars"); ChromeDriver driver = new ChromeDriver(options);
Methods in ChromeOptions class:
Some frequently used ChromeOptions method are:
To add a new Extension:
options.addExtensions(new File (<path of crx>)); – This method is to run chrome browser with an extension. All extensions are stored on your system with a file that has an extension .crx.
In the mentioned method pass the path of crx file and invoke the browser. You will observe that browser is invoked with the mentioned extension.
To add a binary path:
options.setBinary(new File(<path of binary file>)); –
To run chrome browser with same parameters:
There are configurations you can do on your chrome browser by passing some arguments – options.addArguments(“<arguments>”);
To disable the ribbon alert which comes on the chrome browser when the browser is invoked via Selenium use –
To run the chrome browser in headless mode we can use- options.addArguments(“–headless”);
I will add a separate article to make chrome work in headless mode.
To run chrome browser with an untrusted certificate:
In chrome browser, if you are navigating to a website which needs certificates you can add a method to the call flow
Merging ChromeOptions with Desired Capabilities:
Before ChromeOptions we used to use DesiredCapabilities class to control these settings which are deprecated now. But you can still use DesiredCapabilities and can merge them with ChromeOptions.
DesiredCapabilities cap = DesiredCapabilities.chrome(); ChromeOptions options = new ChromeOptions(); options.merge(cap); options.addArguments("disable-infobars");
PS: For any questions, queries, or feedback. Feel free to write us at email@example.com or firstname.lastname@example.org