Table of content

Open Browsers

As selenium support only web-based applications, opening a browser for operation is must. You cannot access already opened browser in selenium

Open Firefox with selenium

WebDriver driver=new FirefoxDriver();

In the above line which opens the Firefox browser, I hope the reader is familiar with creating an object in java.

  • WebDriver - Webdriver is the interface which is inherited from SearchContext
  • new - new is the keyword in java which creates an object (address space)in the heap area of CPU
  • FirefoxDriver() - FirefoxDriver() is a constructor of FirefoxDriver class which implements all the methods in the present in the webdriver interface and this opens the firefox Browser.
  • driver - driver is a reference variable, which refers to the address space created on the heap.
  • ; - line ending statement

We call this process as run-time polymorphism and upcasting in java

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;

public class OpenFirefox
{
	public static void main(String[] args) throws Exception
	{
		WebDriver driver=new FirefoxDriver();
	}
}
Code Example for Opening Firefox [ Selenium 3.0 and above ]
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;

public class OpenFirefox
{
	public static void main(String[] args) throws Exception
	{
		System.setProperty("webdriver.gecko.driver", "c:\path\geckodriver.exe");
		WebDriver driver = new FirefoxDriver();
	}
}
Code Example for Opening Chrome
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;

public class OpenChrome
{
	public static void main(String[] args) throws Exception
	{
		// we have to set the .exe file path to open Google chrome
		System.setProperty("webdriver.chrome.driver", "chromedriver.exet file path till .exe");
		WebDriver driver = new ChromeDriver();
	}
}
Code Example for Opening IE
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.ie.InternetExplorerDriver;

public class OpenIE
{
	public static void main(String[] args) throws Exception
	{
		// we have to set the .exe file path to open Internet Explorer
		System.setProperty("webdriver.ie.driver", "IEDriverServer.exe path till .exe");
		WebDriver driver = new InternetExplorerDriver();
	}
}
Code Example for Opening Opera
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;

public class OpenOpera
{
	public static void main(String[] args) throws Exception
	{
		// we have to set the .exe file path to open Opera
		System.setProperty("webdriver.chrome.driver", "C:/Users/PATH/operadriver-0.1.0-win32.exe");
		WebDriver driver = new ChromeDriver();
	}
}
Code Example for Opening Microsoft Edge
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.edge.EdgeDriver;

public class OpenOpera
{
	public static void main(String[] args) throws Exception
	{
		// we have to set the .exe file path to open ms Edge
		System.setProperty("webdriver.edge.driver", "MicrosoftWebDriver.exe path till .exe");
		WebDriver driver = new EdgeDriver();
	}
}
Code Example for Opening Html Unit Driver
import org.openqa.selenium.WebDriver;
import org.openqa.seleniumunitUnitDriver;

public class OpenOpera
{
	public static void main(String[] args) throws Exception
	{
		// we have to set the .exe file path to open ms Edge
		WebDriver driver = new HtmlUnitDriver();
	}
}

Multiple Inheritance in Java and Selenium

Comment / Suggestion Section
Point our Mistakes and Post Your Suggestions
  • Mahesh
    Reply
  • Hanuma
    It  was nice article. it will be very useful for new selenium learners. 
    Reply