Run firefox , chrome and Internet explorer in Selenium Webdriver

Here we will learn how to run the different browsers in selenium via web driver. For cross browser testing we need to test the application on different browsers so in this article we will learn how we can run chrome , mozilla firefox and internet explorer in selenium.

Selenium supports the following browsers

  1. FireFox
  2. Internet Explorer
  3. Google Chrome
  4. Opera
  5. Safari

Now later we will learn how to run the above browsers. To run the browsers we need the driver for each browser which is available at selenium official website .


Run firefox in Selenium

Steps to run the firefox in selenium

1) Download the geckodriver from the this website.
2) Go the download section of the above website and download the windows version of the gecko driver.
3) unzip the driver and paste the geckodriver.exe file in the target folder of program or give the path of driver in System.setProperty 2nd parameter.
4) Now with the below code we can execute the browser.
5) Package required to run firefox browser : import org.openqa.selenium.firefox.FirefoxDriver;

Java Code to execute the browser

        // first parameter is always same , 2nd parameter is path of driver   
	System.setProperty("webdriver.gecko.driver", "D:\\drivers\\geckodriver.exe");
	WebDriver driver=new FirefoxDriver(); //openbrowser
	driver.get("https://padhle.com/"); //open url

Some times the above code might give exception for some firefox browsers so we need to change the first parameter of System.setProperty as below
System.setProperty(“webdriver.gecko.driver”, “D:\\drivers\\geckodriver.exe”);
to System.setProperty(“webdriver.firefox.marionette”, “D:\\drivers\\geckodriver.exe”);

Java Code for Marionette driver

System.setProperty("webdriver.firefox.marionette", "geckodriver.exe");
		WebDriver driver=new FirefoxDriver(); //openbrowser
		driver.get("https://padhle.com/"); //open url

Note : The second parameter is the path of the driver where you have saved the driver.

Note : If driver is peresent in target folder of Java Program we can simply pass “geckodriver.exe” as second parameter.
System.setProperty(“webdriver.gecko.driver”, “geckodriver.exe”);

Note : Gecko driver is required for the selenium version 3 or above . For the selenium version below 3 we are not required with any driver to run firefox. we can simply launch the browser with below java code.

WebDriver driver=new FirefoxDriver(); //openbrowser
driver.get("https://padhle.com/"); //open url

Note : For all the other browser we required drivers

Note :  if you face any exception while executing mozilla firefox just upgrade the fire fox and download the latest driver.


Run Chrome in selenium

Steps to run the chrome in selenium

1) Download the chrome driver from the official website.
2) Download the windows version of the chrome driver.
3) unzip the driver and paste the chromedriver.exe file in the target folder of program or give the path of driver in System.setProperty in 2nd parameter.
4) Now with the below code we can execute the browser.
5) Package required to run chrome browser : import org.openqa.selenium.chrome.ChromeDriver;

Java Code to run chrome browser in selenium

// first parameter is always same , 2nd parameter is path of driver
System.setProperty("webdriver.chrome.driver", "D:\\drivers\\chromedriver.exe");
WebDriver driver=new ChromeDriver(); //openbrowser
driver.get("https://padhle.com/"); //open url

Note : Sysytem.setProperty( first parameter always same – webdriver.chrome.driver , 2nd parameter is path of driver)


Run Internet Explorer in selenium

Steps to run the internet explorer in selenium

1) Download the internet explorer driver from the official website.
2) Scroll down and download the windows version of the IE driver.
3) unzip the driver and paste the IEDriverServer.exe file in the target folder of program or give the path of driver in System.setProperty in 2nd parameter.
4) Now with the below code we can execute the browser.
5) Package required to run firefox browser : import org.openqa.selenium.ie.InternetExplorerDriver;

Java code to run IE

DesiredCapabilities capabilities = DesiredCapabilities.internetExplorer();
capabilities.setCapability(InternetExplorerDriver.INTRODUCE_FLAKINESS_BY_IGNORING_SECURITY_DOMAINS,true);
System.setProperty("webdriver.ie.driver", "D:\\drivers\\IEDriverServer.exe");
WebDriver driver = new InternetExplorerDriver(capabilities); //openbrowser
driver.get("https://padhle.com/"); //open url

Note : To execute internet explorer we need to disable all the security checks in Internet explorer settings but in corporate the security tab is disabled so we need to use the capabilities to execute the Internet explorer.


Execute all browsers in one program , now save all the drivers in the target folder of the project , so we need not to give the whole path. This below code we will use later on while making Hybrid frameworks , Data Driven frameworks and POM.

Java Program to execute the all browsers by passing parameters

package com.SeleniumBasics;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.ie.InternetExplorerDriver;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.testng.annotations.Test;

public class Working_With_Browsers {

	public WebDriver driver;
	// parameter
	public String Browser="ie";
	
	@Test
	public void workwithbrowsers() throws InterruptedException{
		
		// browser name is mozilla
		if(Browser.equalsIgnoreCase("mozilla")){
			System.setProperty("webdriver.gecko.driver", "geckodriver.exe");
			//System.setProperty("webdriver.firefox.marionette", "geckodriver.exe");
			driver=new FirefoxDriver(); //openbrowser
		}
		// browser name is chrome
		else if(Browser.equalsIgnoreCase("chrome")){
			System.setProperty("webdriver.chrome.driver", "chromedriver.exe");
			 driver=new ChromeDriver();
		}
		// browser name is internet explorer
		else if(Browser.equalsIgnoreCase("ie")){
			DesiredCapabilities capabilities = DesiredCapabilities.internetExplorer();
			capabilities.setCapability(InternetExplorerDriver.INTRODUCE_FLAKINESS_BY_IGNORING_SECURITY_DOMAINS,true);
			System.setProperty("webdriver.ie.driver", "IEDriverServer.exe");
			 driver=new InternetExplorerDriver(capabilities); //openbrowser
		}
		driver.get("https://padhle.com/"); //open url
		driver.manage().window().maximize(); // maximize the window
		Thread.sleep(10);
		driver.quit();
		
	}
}

Note : All driver are present in project folder so no need to give the complete path and it is good practice so when this code is run on some other machine no need to change the path.

run browser in selenium

This is all about how to run the different browsers in selenium through webdriver. Similarly we can run for the opera and safari browsers. If you have any question please leave in the below comments.