Writing the First Program in Selenium

In the previous post, we have seen the difference between findelement and findElements methods in selenium. In this post, we will write the first program in selenium.

We have already learnt how to create the project and class in Eclipse. It is assumed that you have already configure Selenium as already explained in the earlier post.We will perform login functionality in Gmail application. To start with this, we need to perform the following steps.

1. Open the Gmail application in Firefox browser.
2. Make sure , Sign In page is opened.
3. Press F12 to open firebug. We need to identify the properties of the elements like username,password ,button etc.

Sign In page

4. Create the object of FirefoxDriver and open Gmail application using get() method.

WebDriver driver = new FirefoxDriver();

5. Inspect the element Username in the application with the help of Firebug. As we can see, it is having id attribute as “Email“. We will use this to identify username.

Object identify in selenium

How to use this id in selenium to find this element?

For that we use a method named ‘findelement’, where we need to pass locator and its value for creating that particular element. We have use use method to perform some operations according to the type of element. As username is a edit box and we enter the data into that. So, there is method we will use called ‘sendKeys’ which enters the data into edit box.

e.g. We are entering email id “[email protected]” by using below line of code.

driver.findElement(By.id(“Email”)).sendKeys(“[email protected]”);

6. Same way, we will inspect the another element which is a next button. We need to click on this button to proceed. We will identify its properties as well. But here we will try with another locator which is XPATH. The XPATH of next button is “//*[@id=’next’]“.

Next Button


As it is a button, action perform on this element will be Click().

7. Similar way, we will create object for Password and Sign In button. You can use any unique property for those elements.

Click on button in selenium


driver.findElement(By.id(“Passwd”)).sendKeys(“12345”); driver.findElement(By.id(“signIn”)).click();


We have used Thread.Sleep(3000L) which will pause the execution for 3 seconds before executing next step. It will add an exception(InterruptedException) in method signature using throws keyword.

Now, Run the program and it should login into the Gmail application.

Full Code of Login into Gmail:

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

public class LoginGmail {
	private static WebDriver driver;
	public static void main(String[] args) throws InterruptedException {
		driver = new FirefoxDriver();
		driver.findElement(By.id("Email")).sendKeys("[email protected]");
		Thread.sleep(3000L);  // Thread.sleep is used to put some wait before executing next step (3 seconds)


Ask Question
Have any question or suggestion for us?Please feel free to post in  Q&A Forum
Avatar photo

Shekhar Sharma

Shekhar Sharma is founder of testingpool.com. This website is his window to the world. He believes that ,"Knowledge increases by sharing but not by saving".

You may also like...

1 Response

  1. August 16, 2015

    […] the previous post, we have written a simple program. In this post, we will learn about the close() and Quit() method in […]