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
Quickly go to:-
- Launching Firefox Using Selenium Webdriver
- Launching Chrome Using Selenium Webdriver
- Launching Safari Using Selenium Webdriver
- Launching Microsoft Edge Using Selenium Webdriver
- Launching Internet Explorer Using Selenium Webdriver
- Launching Opera Using Selenium Webdriver
- Launching HTML Unit Using Selenium Webdriver
- And Finally
- See Also:-
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.
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:-
<dependency> <groupId>org.seleniumhq.selenium</groupId> <artifactId>selenium-htmlunit-driver</artifactId> <version>2.52.0</version> </dependency>
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);
WebDriver HTMLUnitdriver = new HtmlUnitDriver(true);
WebDriver HTMLUnitdriver = new HtmlUnitDriver(BrowserVersion.FIREFOX, true);
I have written a dedicated post about using HTMLUnit with Selenium.
I hope this tutorial has helped you launch the different browser types using Selenium WebDriver, and see you again in another tutorial.
- Introduction to selenium web driver series. What is it? And what browsers & programming languages it supports – Tech Fairy
- What are 60% Keyboards (65%)? And what are their advantages & disadvantages – Tech Fairy
- 40% Keyboards Form Factor, What are they? And why would you use one? – Tech Fairy
- Why ThinkPad laptops are popular, and what are their advantages?
- What are business-grade laptops? What are their advantages? And which one to buy?
- Java VS Python VS C# detailed comparison, which language to learn first?
- Do you need an Expensive motherboard for gaming?
- What is the meaning of the different USB port colors? (blue, teal blue, yellow, red, black)
- Why motherboards & laptops still come with USB 2.0 ports When USB 3.0 Is Backward Compatible?
- USB 2.0 VS USB 3.0 Comparison: What are the differences between the two ports?