• Selenium Video Tutorials

Selenium WebDriver - Browser Options



The browser options refer to the features and capabilities shared by all browsers. It helps to modify the settings and capabilities of the browser while running an automated test on any browser. Selenium Webdriver begins with a fresh browser profile without any predefined settings on cookies, history, and so on by default.

Example 1 - Adding Extensions to Browser

Let us take an example, where we would launch the Chrome browser with a Selenium IDE extension using the ChromeOptions class. Every Chrome extension should have a .crx file.

Selenium Browser Options 1

We would get the .crx file for Selenium IDE Chrome extension and place it under the Resources folder within our test project.

package org.example;

import org.openqa.selenium.*;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import java.io.File;
import java.util.concurrent.TimeUnit;

public class ChromeOptnsExtension {
   public static void main(String[] args) throws InterruptedException {

      // object of ChromeOptions
      ChromeOptions opt = new ChromeOptions();

      // adding .crx extension
      opt.addExtensions(new File("./Resources/SeleniumIDE.crx"));

      // Initiate the Webdriver
      WebDriver driver = new ChromeDriver(opt);

      // adding implicit wait of 15 secs
      driver.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS);

      // Opening the webpage with Selenium IDE extension
      driver.get("https://www.tutorialspoint.com/selenium/practice/register.php");
   }
}

Output

Process finished with exit code 0

In the above example, we observed that the Chrome browser got launched with the Selenium IDE extension along with the information bar Chrome is being controlled by automated test software.

Selenium Browser Options 2

Finally, the message Process finished with exit code 0 was received, signifying successful execution of the code.

Example 2 - Open Browser in Headless Mode

In this example, we would open and launch an application in the headless browser using the EdgeOptions class.

package org.example;

import org.openqa.selenium.*;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.firefox.FirefoxOptions;
import java.io.File;
import java.util.List;
import java.util.concurrent.TimeUnit;

public class FirefoxOptnsHeadless {
   public static void main(String[] args) throws InterruptedException {

      // object of FirefoxOptions
      FirefoxOptions opt = new FirefoxOptions();

      // open in headless mode
      opt.addArguments("-headless");

      // Initiate the Webdriver
      WebDriver driver = new FirefoxDriver(opt);

      // adding implicit wait of 15 secs
      driver.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS);

      // Opening the webpage with headless mode
      driver.get("https://www.tutorialspoint.com/selenium/practice/selenium_automation_practice.php");

      // getting page title
      System.out.println("Getting the page title: " + driver.getTitle());

      // Quitting browser
      driver.quit();
   }
}

Output

Getting the page title: Selenium Practice - Student Registration Form

Process finished with exit code 0

In the above example, we observed that the Firefox browser was launched in a headless mode. We had also obtained the browser title with the message in the console - Getting the page title: Selenium Practice - Student Registration Form.

Finally, the message Process finished with exit code 0 was received, signifying successful execution of the code.

Example 3 - Create Profiles in Browser

Let us take another example, where we would launch the Firefox browser with our own Firefox profile using the FirefoxOptions class.

package org.example;
package org.example;

import org.openqa.selenium.*;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.firefox.FirefoxOptions;
import org.openqa.selenium.firefox.FirefoxProfile;
import org.openqa.selenium.firefox.ProfilesIni;
import java.util.concurrent.TimeUnit;

public class FirefoxOptnsProfile {
   public static void main(String[] args) throws InterruptedException {

      // object of FirefoxOptions
      FirefoxOptions opt = new FirefoxOptions();

      // object of ProfilesIni
      ProfilesIni prof = new ProfilesIni();

      // object of ProfilesIni
      FirefoxProfile p = prof.getProfile("<profileName>");

      opt.setProfile(p);

      // Initiate the Webdriver
      WebDriver driver = new FirefoxDriver(opt);

      // adding implicit wait of 15 secs
      driver.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS);

      // Opening the webpage
      driver.get("https://www.tutorialspoint.com/selenium/practice/selenium_automation_practice.php");

      // getting page title
      System.out.println("Getting the page title: " + driver.getTitle());

      // Quitting browser
      driver.quit();
   }
}

Output

Getting the page title: Selenium Practice - Student Registration Form

Process finished with exit code 0

In the above example, we observed that the Firefox browser was launched with a created profile. We had also obtained the browser title with the message in the console - Getting the page title: Selenium Practice - Student Registration Form.

Finally, the message Process finished with exit code 0 was received, signifying successful execution of the code.

Example 4 - Handle SSL Certificate Errors

To handle SSL certificates in Edge, we have to use the EdgeOptions class along with the DesiredCapabilities class. SSL error shown in Edge is Your connection isn’t private.

package org.example;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.EdgeDriver;
import org.openqa.selenium.firefox.EdgeOptions;
import org.openqa.selenium.remote.CapabilityType;
import org.openqa.selenium.remote.DesiredCapabilities;
import java.util.concurrent.TimeUnit;

public class SSLErrorEdge {
   public static void main(String[] args) throws InterruptedException {

      // Desired Capabilities class to profile Edge
      DesiredCapabilities dc = new DesiredCapabilities();

      // Desired Capabilities class to profile Edge
      DesiredCapabilities dc = new DesiredCapabilities();
      dc.setCapability(CapabilityType.ACCEPT_INSECURE_CERTS, true);

      // Edge Options class
      EdgeOptionsOptions opt = new EdgeOptions();
      opt.merge(dc);

      // Initiate the Webdriver with options
      WebDriver driver = new EdgeDriver(opt);

      // adding implicit wait of 15 secs
      driver.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS);

      // launch application with SSL error
      driver.get("https://expired.badssl.com");

      // get browser title
      System.out.println("Browser title in Edge: " + driver.getTitle());

      // quitting the browser
      driver.quit();
   }
}

Output

Browser title in Edge: Privacy error

Process finished with exit code 0

In the above example, we had the SSL certificate error in Edge and launched the application and then obtained the browser title with the message in the console - Browser title in Edge: Privacy error.

Finally, the message Process finished with exit code 0 was received, signifying successful execution of the code.

Example 5 - Page Loading Strategy

There are multiple page loading strategies available in the browser, they are NORMAL(default page load setting in browser), EAGER, and NONE.

package org.example;

import org.openqa.selenium.*;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;

public class ChromePageLoadNormal {
   public static void main(String[] args) throws InterruptedException {

      // object of ChromeOptions
      ChromeOptions opt = new ChromeOptions();

      // setting pageloadStrategy to normal
      opt.setPageLoadStrategy(PageLoadStrategy.NORMAL);

      // Initiate the Webdriver
      WebDriver driver = new ChromeDriver(opt);

      // Opening the webpage
      driver.get("https://www.tutorialspoint.com/selenium/practice/register.php");

      // quitting browser
      driver.quit();
   }
}

Output

Process finished with exit code 0

In the above example, we had set the browser option set such that page load strategy is set to NORMAL. In case, we need to set the page load strategy is set to EAGER, we have to make the below code change −

// object of ChromeOptions
ChromeOptions opt = new ChromeOptions();

// setting pageloadStrategy to EAGER
opt.setPageLoadStrategy(PageLoadStrategy.EAGER);

// Initiate the Webdriver
WebDriver driver = new ChromeDriver(opt);

In case, we need to set the page load strategy is set to NONE, we have to make the below code change −

// object of ChromeOptions
ChromeOptions opt = new ChromeOptions();

// setting pageloadStrategy to NONE
opt.setPageLoadStrategy(PageLoadStrategy.NONE);

// Initiate the Webdriver
WebDriver driver = new ChromeDriver(opt);

This concludes our comprehensive take on the tutorial on Selenium Webdriver - Browser Options. We’ve walked through examples of how to add profiles and extensions to browsers, how to launch the browser in headless mode, how to handle SSL certificates errors, and page loading strategies with browser options along with Selenium Webdriver.

This equips you with in-depth knowledge of the browser options in Selenium Webdriver. It is wise to keep practicing what you’ve learned and exploring others relevant to Selenium to deepen your understanding and expand your horizons.

Advertisements