top of page

Selenium Java Tutorial

First Selenium Java Program

How To Write First Selenium Java Program ?

Launching Web Browser using Selenium WebDriver

Importing Packages
To get started, you need to import following two packages:
  1. org.openqa.selenium.* - contains the WebDriver class needed to instantiate a new browser loaded with a specific driver.

  2. org.openqa.selenium.firefox.FirefoxDriver - contains the FirefoxDriver class needed to instantiate a Firefox-specific driver onto the browser instantiated by the WebDriver class.



Instantiating objects and variables

Normally, this is how a driver object is instantiated.

WebDriver driver = new FirefoxDriver();

A FirefoxDriver class with no parameters means that the default Firefox profile will be launched by our Java program. The default Firefox profile is similar to launching Firefox in safe mode.
For convenience, we saved the Base URL and the expected title as variables.
Launching a Browser Session
WebDriver's get() method is used to launch a new browser session and directs it to the URL that you specify as its parameter.

driver.get(baseURL);

Terminating a Browser Session
The "close()" method is used to close the browser window.

driver.close();

We Need to download 'geckodriver' for Firefox browser - Download
A Simple Java Code For Firefox Browser =>

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

public class FirstSeleniumJavaProgram { public static void main(String ars[]){

System.setProperty("webdriver.gecko.driver", "drivers/geckodriver.exe");

WebDriver driver = new FirefoxDriver(); String baseurl = "https://google.com/"; System.out.println("Launching firefox");

driver.get(baseurl); driver.close();

} }





We Need to download 'chromedriver' for Chrome browser - Download
A Simple Java Code For Chrome Browser =>

import org.openqa.selenium.WebDriver; import org.openqa.selenium.chrome.chromeDriver;

public class FirstSeleniumJavaProgram { public static void main(String ars[]){

System.setProperty("webdriver.chrome.driver", "E://chromedriver.exe");

WebDriver driver = new ChromeDriver(); String baseurl = "https://www.google.com/"; System.out.println("Launching Chrome Broswer");

driver.get(baseurl); driver.close();

} }



A Complete Java Program to launch google and search anything =>

import org.jboss.netty.util.internal.SystemPropertyUtil; import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.chrome.ChromeDriver;

public class FirstSeleniumJavaProgram {

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

System.setProperty("webdriver.chrome.driver", "drivers/chromedriver.exe"); WebDriver driver = new ChromeDriver(); String baseurl = "https://www.google.com/"; System.out.println("Hitting 'https://www.google.com/' on chrome browser"); driver.get(baseurl); WebElement element =driver.findElement(By.xpath("//input[@name='q']")); element.clear(); element.sendKeys("Google Maps"); System.out.println("Searching 'Google Maps' in google search map"); System.out.println("Waiting for 5 sec"); driver.close(); System.out.println("closed chrome browser");

} }

Refer next page WebDriver Commands
bottom of page