Selenium WebDriver Tutorial

Posted on by By admin, in Miscellaneous, Software Testing | 0

What is Selenium?

In lay mans terms, selenium is a program which we can use to automate web page related tasks by programmatically replicating the manual tasks of key presses and mouse clicks.

How to use Selenium in Eclipse?

For Selenium to be used in Eclipse IDE, the IDE should be able to access the classes specified in the Selenium Package. This can be achieved by either adding the JAR file as an external library or adding the maven dependency in pom.xml.

The JAR file and the dependency can be downloaded from :


How to code for testing in Selenium?

To get the Test Script working, the following steps need to be followed:

  1. Add Selenium to Eclipse IDE.
  2. Import the required classes (WebDrivers).
  3. Declare a WebDriver object.
  4. Fetch the URL.
  5. Find the Required component.
  6. Perform the required action (click, enter key-strokes, etc.).
  7. Close the WebDriver.


Adding Selenium to Eclipse IDE:

Mentioned in “How to use Selenium with Eclipse?”.

Import the Required Classes:

To create a Browser instance, a “WebDriver” object needs to be created. For this “org.openqa.selenium.WebDriver”needs to be imported. Since Selenium uses “Firefox” browser as its default browsing engine, to us any other browser such as “Chrome”, we need to import its supporting class separately (

While using Chrome, the driver for it, “chromdriver.exe”, needs to be downloaded separately and called in the class where the WebDriver is being declared.

This is done as follows:

“setProperty(“”, “pathToFile/chromedriver.exe”);”


Declare a WebDriver Object:

Declaring the WebDriver is done as follows-

“WebDriver driver;

driver = new ChromeDriver();


driver = new FirefoxDriver();”

/*Depending on which browser is being used*/


Fetch the URL:

The browser is opened and page is fetched by using the following code:


Once the URL is opened, any required action on its components can be performed.


Perform Required Actions:

  1. Finding elements and Clicking on them – Elements can be found using their CSS or XPATH.

Css Selector-  “driver.findElement(By.cssSelector(“<cssSelector>”)).click();”

XPATH Selector-  “driver.findElement(By.xpath(“<xPath>”)).click();”


  1. Filling data in text field – For this the text field must be found and cleared before new text is added to it.

Clearing Field – “driver.findElement(By.xpath(“<xpath>”)).clear();”

Adding Key-Strokes – “driver.findElement(By.xpath(“<xpath>”)).sendKeys(“AutomatedTest”);”


  1. Drop-Down Menu – To access the drop down menu, the following method works best.

“Select droplist = new Select(driver.findElement(By.cssSelector(“<cssSelector>”)));

droplist.selectByVisibleText(“<visible text of menu to be selected>”);”


  1. Waiting for page or component load – At times, the program needs to wait for new page or modal windows to load before continuing. For this the following code is to be used.

“Thread.sleep(<time in miliseconds>);”


  1. Using the “Enter” key – At times, its easier to use the “Enter” key than to click on “ok” or “submit”.

“driver.findElement(By.xpath(“<xpath of last changed component>”)) .sendKeys(Keys.RETURN);”


Important Notes:

The code must always be included in try – catch blocks.

The page should be given time to load.

Css and xPath do not always work for every component in every browser so use whichever one is working.




Best Open Source Business Intelligence Software Helical Insight is Here


A Business Intelligence Framework

0 0 votes
Article Rating
Notify of
Inline Feedbacks
View all comments