Click here to Skip to main content
14,828,012 members
Articles / Web Development
Posted 24 Oct 2014

Tagged as


10 bookmarked

A Small Java Script Using Selenium Webdriver

Rate me:
Please Sign up or sign in to vote.
4.75/5 (8 votes)
24 Oct 2014CPOL2 min read
A small Java script using Selenium Webdriver

In the previous post, we had discussed about the installation of Java, and configuration of Eclipse with selenium WebDriver. Now here, we will be creating a small Java script. All beginners will first want to open browser and to automate it. So here, we will be doing that.

First of all, we will write the scenario of what we will be doing here. Here, we will Login to Gmail account and will automate the below scenarios.

  1. Open a Firefox browser
  2. Navigate to the URL
  3. Maximize the window
  4. Enter the User Name and Password
  5. Sign-In to the Gmail Account
  6. Click on the Compose Button
  7. Sign-Out from the gmail account
  8. Close the browser

Now we will automate the above scenario. In the previous post, I had discussed about creating a Java Project, Package, Class. And how to import the JAR files into the project. Here also, we have followed the same thing. For example, we will create a Project called “Gmail” , Package as “login”, Class as “Login1″. Now, we will write the code as below:

package login;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
public class Login1 {
public static void main(String[] args) {
// Create a new instance of the Firefox driver
WebDriver driver = new FirefoxDriver();
//  Wait For Page To Load
// Put a Implicit wait, this means that any search for elements on the page
could take the time the implicit wait is set for before throwing exception 
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
// Navigate to URL
// Maximize the window.
// Enter UserName
driver.findElement("Email")).sendKeys(" YOUR USER NAME");
// Enter Password
driver.findElement("Passwd")).sendKeys("YOUR PASSWORD");
// Wait For Page To Load
driver.manage().timeouts().implicitlyWait(60, TimeUnit.SECONDS);
// Click on 'Sign In' button
//Click on Compose Mail.
// Click on the image icon present in the top right navigational Bar
driver.findElement(By.xpath("//div[@class='gb_1 gb_3a gb_nc gb_e']/div/a")).click();
//Click on 'Logout' Button
//Close the browser.
  • After writing this script, you will see some RED lines under some element like this->


  • By hovering over the Bulb symbol present in the left side, you can know which type of error it is.


  • You just need to hover the mouse pointer on that particular element or click on that particular element and press [Ctrl+Space] on the key board. Many suggestions will be listed there and you have to select the appropriate package for that element. To know which Class belongs to which Package, click on See below for the images in details.




  • Now run the script by right clicking on the class ->Run As -> Java Application.


  • Click on the Run Icon present in the Top Navigational Bar.


    Now, you will see the browser will automatically open and will perform the desired task as mentioned above.

So in this article, we discussed about automating the Gmail Login and Sign-out functionality using WebDriver.

Hope this helps beginners like me… :)


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Sipra Ray
Tester / Quality Assurance
India India
I am Sipra Ray, ISTQB certified - Quality Analyst at Mindfire Solutions.
An extrovert by nature ,I like to interact with people and share with all what I learn.
I would love to share and answer queries and gain knowledge from forums like Code Project
Follow up with my blogs at:-

Comments and Discussions

Questionregarding find_element in selenium Pin
Member 140823918-Dec-18 10:01
MemberMember 140823918-Dec-18 10:01 
QuestionSelenium installation Pin
Member 1355131330-Nov-17 20:16
MemberMember 1355131330-Nov-17 20:16 
QuestionAfter giving login button xpath, not able to click on Login button Pin
Member 1314756223-Apr-17 20:34
MemberMember 1314756223-Apr-17 20:34 
QuestionFacing an Error Pin
Member 128326784-Nov-16 3:18
MemberMember 128326784-Nov-16 3:18 
AnswerRe: Facing an Error Pin
Sourabh989018-Feb-17 18:44
MemberSourabh989018-Feb-17 18:44 
QuestionRegarding WebDriver libraray Pin
Member 1232287511-Feb-16 22:59
MemberMember 1232287511-Feb-16 22:59 
AnswerRe: Regarding WebDriver libraray Pin
Sourabh989018-Feb-17 19:59
MemberSourabh989018-Feb-17 19:59 
Questionhow to open particular mail in gmail Pin
Member 1206042114-Oct-15 23:27
MemberMember 1206042114-Oct-15 23:27 
QuestionSkype Pin
Nắng Hồng21-Sep-15 18:59
MemberNắng Hồng21-Sep-15 18:59 
Questionprogram not able to find the "Passwd" element id in the Gmail Login program and giving exception Pin
learner1259-Aug-15 1:02
Memberlearner1259-Aug-15 1:02 
AnswerRe: program not able to find the "Passwd" element id in the Gmail Login program and giving exception Pin
Sourabh989013-Aug-15 1:35
MemberSourabh989013-Aug-15 1:35 
Answergmail login and logout program Pin
sachit007recreated4-Oct-15 22:59
Membersachit007recreated4-Oct-15 22:59 
GeneralRe: gmail login and logout program Pin
Member 121951709-Dec-15 17:18
MemberMember 121951709-Dec-15 17:18 
QuestionUnable to logout from Gmail Using your script Pin
Swati Bhatt1-Apr-15 22:48
MemberSwati Bhatt1-Apr-15 22:48 
AnswerRe: Unable to logout from Gmail Using your script Pin
Member 118899245-Aug-15 22:43
MemberMember 118899245-Aug-15 22:43 
GeneralMy vote of 4 Pin
Jay Bardeleben30-Dec-14 3:51
professionalJay Bardeleben30-Dec-14 3:51 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.