Below example shows a way to open Chrome browser in maximized mode using ChromeOptions class. We need to pass an instance of ChromeOptions class to the web driver initialization. Below are the list of available and most commonly used arguments for ChromeOptions class

start-maximized: Opens Chrome in maximize mode incognito: Opens Chrome in incognito mode headless: Opens Chrome in headless mode disable-extensions: Disables existing extensions on Chrome browser disable-popup-blocking: Disables pop-ups displayed on Chrome browser make-default-browser: Makes Chrome default browser version: Prints chrome browser version disable-infobars: Prevents Chrome from displaying the notification ‘Chrome is being controlled by automated software

Example: Below are the most commonly used pre-defined capability types.

Chrome Options for Adblocker extension

Adblocker extension of the Chrome browser can be handled using ChromeDriver Options and Desired Capabilities class. Below are the steps to access AdBlocker extension on the Chrome browser using Desired Capabilities class. Step 1) AdBlocker extension must be installed on Chrome browser before using Chrome Options class Step 2) Extract the CRX File corresponding to AdBlocker extension through http://crxextractor.com/ Step 3) Pass the downloaded CRX File path to Chrome Options class Step 4) Instantiate the web driver using the desired capabilities class and Chrome Options in Selenium object Example: Below example demonstrates how to activate ad blocker extension on the Chrome browser using Chrome Options and Desired Capabilities class.

Extract CRX File:

Below steps demonstrate the process of extracting CRX File through Ad Blocker through the web site – http://crxextractor.com/ Step 1) Go to http://crxextractor.com/ and click start button

Step 2) Enter the chrome extension – Ad Blocker URL under the textbox. URL for Adblock on Chrome web store is https://chrome.google.com/webstore/detail/adblock-%E2%80%94-best-ad-blocker/gighmmpiobklfepjocnamgkkbiglidom and click ok

Step 3) On clicking the OK Button, the label of the button will change to Get .CRX as below. Click on Get .CRX button, CRX file corresponding to the extension will be downloaded

Step 4) Save the file onto the local machine, make a note of the path saved. The next step is to pass the saved path to Chrome Options class

Sample Code:

You will see ads at http://demo.guru99.com/ as below

With AdBlocker extension enabled on Chrome browser ads should be disabled

Code Explanation:

Initially, you need to set the path to the chromedriver.exe file using set property method since you are using Chrome Browser for testing You need to set the path to CRX File to add extensions method Then you need to create an object of Chrome Desired Capabilities in Selenium class and pass it to web driver instance. From Selenium 3.8.1 version, driver capabilities class is deprecated and you need to merge capabilities object with Chrome Options object before passing the same as an argument to Chrome Driver constructor Open the URL – http://demo.guru99.com/test/simple_context_menu.html with Ad Blocker extension enabled Maximize and close the browser

NOTE: We are enabling AdBlocker extension on the Chrome browser through automation script instead of manually enabling Adblocker extension on the Chrome browser. CRX File is a way to access ad blocker extension using automation script Output: Chrome browser will be enabled with AdBlocker extension enabled as below without any ads

Chrome Options for Incognito mode

Chrome Options can be used for incognito mode by using the pre-defined argument –incognito. Below is the sample code to accomplish the same. Sample Code: Code Explanation:

Initially, you need to set the path to the chromedriver.exe file using set property method since you are using Chrome Browser for testing Then you need to create an object of Chrome Options class and pass it to web driver instance. Since we want to open Chrome browser in incognito mode, you need to pass the argument –incognito to Chrome Options class. Next, create an object of Desired Capabilities class and merge the Desired Capabilities class object with Chrome Options class object using merge method You need to create an object of Chrome Driver class and pass the Chrome Options object as an argument Finally, we need to pass the URL – http://demo.guru99.com/test/simple_context_menu.html to the driver.get method Maximize and close the browser

Output: The chrome browser window will be opened in Incognito mode as below

Chrome Options for Headless Chrome

A Headless browser runs in the background. You will not see the browser GUI or the operations been operated on it. Chrome Options for running Chrome browser in headless mode can be accomplished by using the predefined arguments –headless. Sample code to accomplish it is mentioned below. Example: Code Explanation:

Initially, you need to set the path to the chromedriver.exe file using set property method since you are using Chrome Browser for testing Next, create an object of Chrome Options class and pass it to web driver instance. Since we want to open Chrome browser in headless mode, we need to pass the argument –headless to Chrome Options class. Create an object of DesiredCapabilities Chrome class and merge the Desired Capabilities class object with Chrome Options class object using merge method Create an object of Chrome Driver class and pass the Chrome Options Selenium object as an argument Finally, we need to pass the URL – http://demo.guru99.com/ to the driver.get method Print the page title and close the browser

Output The browser will not be visible for the above code as Chrome will be working in Headless mode. Page title will be fetched and displayed as below.

Summary:

Selenium Chrome Options class is used to manipulate various properties of Chrome driver Desired Chrome Capabilities class provides a set of key-value pairs to modify individual properties of web driver such as browser name, browser platform, etc. To manipulate any extensions of Chrome browser, CRX File corresponding to the extension must be extracted and must be added to Chrome Options class –incognito and –headless are predefined arguments provided by Chrome Options class for using Chrome browser in incognito mode and headless mode