Check out my iOS apps here
As an Amazon Associate I earn from qualifying purchases.

How to launch any browser using Selenium Web Driver & Java:- Chrome, Firefox, Safari, Microsoft Edge, Internet Explorer, HTML Unit & Opera

Lets Learn How To Launch Almost Any Browser Using Selenium Web Driver

In the Getting Started With Selenium WebDriver tutorial. We used the WebDriver to Launch Firefox and open pages with it. For the sake of simplicity, we have been using it in the rest of the series. But chances that you want to launch some other browser once you start using Selenium in your projects. Either because it’s what the users of your site use, or because you want to test the website using all the commonly used browsers out there, or maybe because it’s what works the best in your case. In this lesson, I will show you how to launch most of the common browsers with the Web Driver. Once you launched the browser, you could use anything we learned in this series about Selenium Web Driver to interact with it.

This lesson is part of a series about Selenium Web Driver. You can go to the first part in the series by clicking here.. For setting up selenium & launching the first test & automated script, you can refer to the first lesson in the series:- Getting Started With Selenium WebDriver tutorial

Launching Firefox Using Selenium Webdriver

As mentioned earlier, Firefox is the browser we use thorough this series. We have seen how you only need to set the Gecko Driver system property to the path of Gecko driver, then create a new FirefoxDriver instance.

You can download Gecko Driver from here.

String GeckoPath = "C:\\Fairy\\geckodriver.exe";
System.setProperty("webdriver.gecko.driver", GeckoPath);
WebDriver driver = new FirefoxDriver();
driver.get("https://tech-fairy.com");

Launching Chrome Using Selenium Webdriver

To launch Chrome Web Driver, we need to download the Chrome driver from here, then tell Selenium where to find it by setting the webdriver.chrome.driver system property.

Full Code:-

System.setProperty("webdriver.chrome.driver", "C:\\Fairy\\chromedriver.exe");
WebDriver Chrome = new ChromeDriver();

Launching Safari Using Selenium Webdriver

As of the time of me writing this tutorial, Safari is no longer available for Windows. So your only option to use it with selenium is on Mac OS systems (or Mac OS X). You could also use Selenium grid to launch Safari instances, but that’s outside the scope of this tutorial.

To use Safari with Selenium, you need to show the Develop menu first. This can be done from the advanced options in Safari preferences:-

After that, toggle Remote Automation on from the Develop menu we just shown:-

Once we enabled Remote Automation, we can simply create an instance of SafariDriver, then interact with it just like any WebDriver instance:-

WebDriver driver = new SafariDriver();
driver.get("http://tech-fairy.com/");
driver.close();

I have written a dedicated lesson that explains these steps in details. ☇

Launching Microsoft Edge Using Selenium Webdriver

Launching Microsoft Edge requires you to Download the Edge driver for your Edge version here. Then specify the path by setting the webdriver.edge.driver property with the driver path.

Bear in mind that in the Edge Driver download page X64 refers to 64-bit Windows machines, while X86 refers to the 32-bit ones.

After that, you simply create an EdgeDriver instance to launch the browser.

System.setProperty("webdriver.edge.driver", "C:\\Fairy\\msedgedriver.exe");
WebDriver Edge = new EdgeDriver();



Launching Internet Explorer Using Selenium Webdriver

To launch Internet Explorer using Selenium Web Driver, go to Selenium downloads page , then scroll down to the Internet Explorer Driver Server section. From there, download the 32-Bit Or 64-bit IE Driver.

Once you have downloaded the driver, you need to set the webdriver.ie.driver property to the path of the driver you downloaded. Then simply create an instance of InternetExplorerDriver to launch the browser:-

System.setProperty("webdriver.ie.driver", "C:\\Fairy\\IEDriverServer.exe");
WebDriver ie = new InternetExplorerDriver();

Launching Opera Using Selenium Webdriver

Launching Opera using web driver is more tricky than the other browsers, since it requires you to specify few things:-

    • The path to the opera driver, which you can download from here. After that, you need you set the webdriver.opera.driver property to the path to it.
    • The path to the Opera binary where you installed Opera. It’s located in C:\Users\\AppData\Local\Programs\Opera by default. Where is the name of your windows username.
    • Launch Opera by creating an instance of OperaDriver.

Important note:- When you specify the opera binary, it should be the path to opera.exe, and not launcher.exe

Here’s the full code for launching Opera Web Driver:-

String OperaPath="C:\\Fairy\\operadriver.exe"; // Path to the Web Driver.
System.setProperty("webdriver.opera.driver" , OperaPath  );
OperaOptions options = new OperaOptions();
options.setBinary("C:\\Opera\\72.0.3815.148\\opera.exe"); // Path to Opera binary. 72.0.3815.148 is the version of Opera.
WebDriver Opera = new OperaDriver(options);

Launching HTML Unit Using Selenium Webdriver

HTML Unit is one of multiple ways to launch headless browser, which is a browser without a user interface, using Selenium Web Driver. Such browsers take less resources, since they don’t have to render the web elements visually on the screen.

To use the HTML Unit browser, simply add the following Maven or Gradle dependency to your project:-

Maven:-

<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-htmlunit-driver</artifactId>
<version>2.52.0</version>
</dependency>

Gradle:-

 compile group: 'org.seleniumhq.selenium', name: 'selenium-htmlunit-driver', version: '2.52.0'

After that, create a new instance of HtmlUnitDriver. You can use the getTitle() function to get the page title & print it to console, since that’s the easiest way to make sure the page was loaded correctly:-

WebDriver HTMLUnitdriver = new HtmlUnitDriver();
HTMLUnitdriver.get("http://yahoo.com/");
String PageTitle = HTMLUnitdriver.getTitle();
System.out.println(PageTitle);

You can specify what browser HTML Unit acts like in the constructor. We do that for Firefox here:-

WebDriver HTMLUnitdriver = new HtmlUnitDriver(BrowserVersion.FIREFOX);

HTML Unit has JavaScript is disabled by default, you can enable it by specifying that in the constructor:-

WebDriver HTMLUnitdriver = new HtmlUnitDriver(true);

You can enable JavaScript & specify a browser at the same time in the constructor:-

WebDriver HTMLUnitdriver  = new HtmlUnitDriver(BrowserVersion.FIREFOX, true);

I have written a dedicated post about using HTMLUnit with Selenium.

 

And Finally

I hope this tutorial has helped you launch the different browser types using Selenium WebDriver, and see you again in another tutorial.

See Also:-

Leave a Comment

Your email address will not be published. Required fields are marked *