Implementation of Our First WebDriver Script – Selenium WebDriver Tutorial #10

In the previous two tutorials, we made you acquainted with the basic architecture and features of WebDriver and the infrastructure required to get started with Selenium WebDriver. Assuming that you all might have set up the system with all the necessary utilities and packages, we will move further with the implementation of our first WebDriver test script.

Therefore, motioning ahead with the consequent Selenium WebDriver tutorial, we would be creating WebDriver script. We would also scatter the light on the basic and commonly used WebDriver commands. We would also learn about the locating strategies of UI elements and their inclusion in the test scripts. We would also study Get Commands in the detail.

Selenium WebDriver Tutorial

Script Creation

For script creation, we would be using “Learning_Selenium” project created in the previous tutorial and “gmail.com” as the application under test (AUT).

Scenario:

  • Launch the browser and open “Gmail.com”.
  • Verify the title of the page and print the verification result.
  • Enter the username and Password.
  • Click on the Sign in button.
  • Close the web browser.

Step 1: Create a new java class named as “Gmail_Login” under the “Learning_Selenium” project.

Step 2: Copy and paste the below code in the “Gmail_Login.java” class.

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

public class Gmail_Login {
/**
* @param args
*/
       public static void main(String[] args) {
             
// objects and variables instantiation
              WebDriver driver = new FirefoxDriver();
              String appUrl = "https://accounts.google.com";
             
// launch the firefox browser and open the application url
              driver.get(appUrl);
             
// maximize the browser window
              driver.manage().window().maximize();
             
// declare and initialize the variable to store the expected title of the webpage.
              String expectedTitle = " Sign in - Google Accounts ";
             
// fetch the title of the web page and save it into a string variable
              String actualTitle = driver.getTitle();
             
// compare the expected title of the page with the actual title of the page and print the result
              if (expectedTitle.equals(actualTitle))
              {
                     System.out.println("Verification Successful - The correct title is displayed on the web page.");
              }
             else
              {
                     System.out.println("Verification Failed - An incorrect title is displayed on the web page.");
              }

// enter a valid username in the email textbox
              WebElement username = driver.findElement(By.id("Email"));
              username.clear();
              username.sendKeys("TestSelenium");
             
// enter a valid password in the password textbox
              WebElement password = driver.findElement(By.id("Passwd"));
              password.clear();
              password.sendKeys("password123");
             
// click on the Sign in button
              WebElement SignInButton = driver.findElement(By.id("signIn"));
              SignInButton.click();
             
// close the web browser
              driver.close();
              System.out.println("Test script executed successfully.");
             
// terminate the program
              System.exit(0);
       }
}

The above code is equivalent to the textual scenario presented earlier.

Code Walkthrough

Import Statements:

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

Prior to the actual scripting, we need to import the above packages:

import org.openqa.selenium.WebDriver – References the WebDriver interface which is required to instantiate a new web browser.

import org.openqa.selenium.firefox.FirefoxDriver – References the FirefoxDriver class that is required instantiate a Firefox specific driver on the browser instance instantiated using WebDriver interface.

import org.openqa.selenium.WebElement – References to the WebElement class which is required to instantiate a new web element.

import org.openqa.selenium.By – References to the By class on which a locator type is called.

As and when our project would grow, it is evident and logical that we might have to introduce several other packages for more complex and distinct functionalities like excel manipulations, database connectivity, logging, assertions etc.

Object Instantiation

WebDriver driver = new FirefoxDriver();

We create a reference variable for WebDriver interface and instantiate it using FirefoxDriver class. A default Firefox profile will be launched which means that no extensions and plugins would be loaded with the Firefox instance and that it runs in the safe mode.

Launching the Web browser

driver.get(appUrl);

A get() method is called on the WebDriver instance to launch a fresh web browser instance. The string character sequence passed as a parameter into the get() method redirects the launched web browser instance to the application URL.

Maximize Browser Window

driver.manage().window().maximize();

The maximize() method is used to maximize the browser window soon after it is re-directed to the application URL.

Fetch the page Title

driver.getTitle();

The getTitle() method is used to fetch the title of the current web page. Thus, the fetched title can be loaded to a string variable.

Comparison between Expected and Actual Values:

if (expectedTitle.equals(actualTitle))
              {
                     System.out.println("Verification Successful - The correct title is displayed on the web page.");
              }
              else
              {
                     System.out.println("Verification Failed - An incorrect title is displayed on the web page.");
              }

The above code uses the conditional statement java constructs to compare the actual value and the expected value. Based on the result obtained, the print statement would be executed.

WebElement Instantiation

WebElement username = driver.findElement(By.id(“Email”));

In the above statement, we instantiate the WebElement reference with the help of “driver.findElement(By.id(“Email”))”. Thus, username can be used to reference the Email textbox on the user interface every time we want to perform some action on it.

Clear Command

username.clear();

The clear() method/command is used to clear the value present in the textbox if any. It also clears the default placeholder value.

sendKeys Command

username.sendKeys(“TestSelenium “);

The sendKeys() method/command is used to enter/type the specified value (within the parentheses ) in the textbox. Notice that the sendKeys() method is called on the WebElement object which was instantiated with the help of element property corresponding to the UI element.

The above block of code enters the string “TestSelenium” inside the Email textbox on the Gmail application.


sendKeys is one of the most popularly used commands across the WebDriver scripts.

Click Command

SignInButton.click();

Like sendKeys(), click() is another excessively used command to interact with the web elements. Click() command/method is used to click on the web element present on the web page.

The above block of code clicks on the “Sign in” button present on the Gmail application.

Notes:

  • Unlike sendKeys() method, click() methods can never be parameterized.
  • At times, clicking on a web element may load a new page altogether. Thus to sustain such cases, click() method is coded in a way to wait until the page is loaded.

Close the Web Browser

driver.close();

The close() is used to close the current browser window.

Terminate the Java Program

System.exit(0);

The Exit() method terminates the Java program forcefully. Thus, remember to close all the browser instances prior terminating the Java Program.

Test Execution

The test script or simply the java program can be executed in the following ways:

#1. Under the Eclipse’s menu bar, there is an icon to execute the test script. Refer the following figure.

WebDriver tutorial 1

Make a note that only the class which is selected would be executed.

#2. Right click anywhere inside the class within the editor, select “Run As” option and click on the “Java Application”.

#3. Another shortcut to execute the test script is – Press ctrl + F11.

At the end of the execution cycle, the print statement “Test script executed successfully.” can be found in the console.

Locating Web Elements

Web elements in WebDriver can be located and inspected in the same way as we did in the previous tutorials of Selenium IDE. Selenium IDE and Firebug can be used to inspect the web element on the GUI. It is highly suggested to use Selenium IDE to find the web elements. Once the web element is successfully found, copy and paste the target value within the WebDriver code. The types of locators and the locating strategies are pretty much the same except for the syntax and their application.

In WebDriver, web elements are located with the help of the dynamic finders (findElement(By.locatorType(“locator value”))).

Sample Code:

driver.findElement(By.id(“Email”));

WebDriver tutorial 2

Locator Types and their Syntax

Locator TypeSyntaxDescription
iddriver.findElement
(By.id(“ID_of_Element”))
Locate by value of
the “id” attribute
classNamedriver.findElement
(By.className
(“Class_of_Element”))
Locate by value of
the “class” attribute
linkTextdriver.findElement
(By.linkText(“Text”))
Locate by value of the
text of the hyperlink
partialLinkTextdriver.findElement
(By.partialLinkText
(“PartialText”))
Locate by value of the
sub-text of the hyperlink
namedriver.findElement
(By.name
(“Name_of_Element”))
Locate by value of the
“name” attribute
xpathdriver.findElement
(By.xpath(“Xpath”))
Locate by value
of the xpath
cssSelectordriver.findElement
(By.cssSelector
(“CSS Selector”))
Locate by value of
the CSS selector
tagNamedriver.findElement
(By.tagName(“input”))
Locate by value of
its tag name

Conclusion

In this tutorial, we developed an automation script using WebDriver and Java. We also discussed the various components that constitute a WebDriver script.

Here are the cruxes of this Selenium WebDriver Tutorial:

  • Prior to the actual scripting, we need to import a few packages to be able to create a WebDriver script.
    • importopenqa.selenium.By;
    • importopenqa.selenium.WebDriver;
    • importopenqa.selenium.WebElement;
    • importopenqa.selenium.firefox.FirefoxDriver;
  • A get() method used to launch a fresh web browser instance. The character sequence passed as a parameter into the get() method redirects the launched web browser instance to the application URL.
  • The maximize() method is used to maximize the browser window.
  • The clear() method is used to clear the value present in the textbox if any.
  • The sendKeys() method is used to enter the specified value in the textbox.
  • Click() method is used to click on the web element present on the web page.
  • In WebDriver, web elements can be located using Dynamic finders.
  • The following are the available locator types:
    • id
    • className
    • name
    • xpath
    • cssSelector
    • linkText
    • partialLinkText
    • tagName

Moving ahead, in the next tutorial, we would shift our focus towards a framework that aids to Automation testing known as TestNG. We would have a detailed study on the various kinds of the annotations provided by the framework.

Next tutorial #11: Before diving deep into Frameworks we will see details about JUnit – an open source unit testing tool. Most of the programmers use JUnit as it is easy and does not take much effort to test. This tutorial will give an insight about JUnit and its usage in selenium script.

A remark for the readers: While our next tutorial of the Selenium series is in the processing mode, readers can start creating their own basic WebDriver scripts. For more advance scripts and concepts, we will have various other Selenium WebDriver tutorials coming up in this series.

Let us know in comments if you have any problem creating or executing the WebDriver scripts.

Recommended Reading


115 thoughts on “Implementation of Our First WebDriver Script – Selenium WebDriver Tutorial #10”

  1. Thanks for the tutorial. Even for the first time user, it seems to be very clear to follow.
    One issue that I have while trying to run the program is the following:

    Exception in thread “main” java.lang.Error: Unresolved compilation problems:
    WebDriver cannot be resolved to a type
    FirefoxDriver cannot be resolved to a type
    WebElement cannot be resolved to a type
    By cannot be resolved
    WebElement cannot be resolved to a type
    By cannot be resolved
    WebElement cannot be resolved to a type
    By cannot be resolved

    at Gmail_Login.main(Gmail_Login.java:13)

    Have I missed something?

  2. Hello,

    Please refer to some code where I need not type the password in clear text in the java code. The password that has been used in this sample code is “password123”. SOme methods for encrypting and use that to string instead. I am also googling on this.

  3. Hello,
    As per many other comments I can see listed here by other users who are using this tutorial, I am also getting the following error:

    “Exception in thread “main” java.lang.Error: Unresolved compilation problems:
    WebDriver cannot be resolved to a type
    FirefoxDriver cannot be resolved to a type
    WebElement cannot be resolved to a type
    By cannot be resolved
    WebElement cannot be resolved to a type
    By cannot be resolved
    WebElement cannot be resolved to a type
    By cannot be resolved

    at First_WebDriverClass.main(First_WebDriverClass.java:18)

    I have added the referenced libraries also, and followed all the steps as and when mentioned in this tutorial.

    Kindly help in resolving this error.
    Also I would like to add, a great job by all the people involved for making this a success.
    Thanks a lot.

  4. Also keep note that now gmail login page design changes and also name got change so please use “Sign in – Google Accounts” this as page name in above example in Original script you can find one blank space at left and right so remove both spaces.

  5. I guess I made a bigger mess while trying to fix the issue.
    Now initially getting the pop-up error

    Error exist in required project(s):
    Learning_Selenium
    Proceed with launch?

    If I do proceed, I get an additional error message:

    Error: A JNI error has occurred, please check your installation and try again.

    Any help???

  6. I have re-installed the eclipse and re-done everything again.
    However came back to the same issue:

    Exception in thread “main” java.lang.Error: Unresolved compilation problems:
    WebDriver cannot be resolved to a type
    FirefoxDriver cannot be resolved to a type
    WebElement cannot be resolved to a type
    By cannot be resolved
    WebElement cannot be resolved to a type
    By cannot be resolved

    at Gmail_Login.main(Gmail_Login.java:13)

    Can someone explain me what is missing or the cause of this issue?????

  7. Hi all, dear friends, Here is the new code with the new Google Signin page , it works fine, you can just copy and paste..

    import org.openqa.selenium.By;

    import org.openqa.selenium.WebDriver;

    import org.openqa.selenium.WebElement;

    import org.openqa.selenium.firefox.FirefoxDriver;

    public class Gmail_Login {

    /**
    8
    * @param args
    9
    * @throws InterruptedException
    */

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

    // objects and variables instantiation

    WebDriver driver = new FirefoxDriver();

    String appUrl = “https://accounts.google.com”;

    // launch the firefox browser and open the application url

    driver.get(appUrl);

    // maximize the browser window

    driver.manage().window().maximize();

    // declare and initialize the variable to store the expected title of the webpage.

    String expectedTitle = “Sign in – Google Accounts”;

    // fetch the title of the web page and save it into a string variable

    String actualTitle = driver.getTitle();
    System.out.println(“title:”+actualTitle);

    // compare the expected title of the page with the actual title of the page and print the result

    if (expectedTitle.equals(actualTitle))

    {

    System.out.println(“Verification Successful – The correct title is displayed on the web page.”);

    }

    else

    {

    System.out.println(“Verification Failed – An incorrect title is displayed on the web page.”);

    }

    // enter a valid username in the email textbox

    WebElement username = driver.findElement(By.id(“Email”));

    username.clear();

    username.sendKeys(“TestSelenium”);
    // Click on next button

    WebElement nxtbtn = driver.findElement(By.id(“next”));
    nxtbtn.click();

    Thread.sleep(1000);
    // enter a valid password in the password textbox

    WebElement password = driver.findElement(By.id(“Passwd”));

    password.clear();

    password.sendKeys(“password123”);

    // click on the Sign in button

    WebElement SignInButton = driver.findElement(By.id(“signIn”));

    SignInButton.click();

    // close the web browser

    driver.close();

    System.out.println(“Test script executed successfully.”);

    // terminate the program

    System.exit(0);

    }

    }

  8. If anyone getting Error something like
    /**——**/
    Exception in thread “main” java.lang.NoClassDefFoundError: com/google/common/base/Function
    ….. /**——**/
    & which is related with the below line,
    /** WebDriver driver = new FirefoxDriver(); **/,

    then please get selenium standalone server version (http://goo.gl/PJUZfa) from this page http://docs.seleniumhq.org/download/ & add this jar to your sample project (Build Path » Configure Build Path » Libraries » Add External JARs). Hope this’ll help resolving the issue..Cheers!!

  9. Am getting an error “unable to locate an element.”

    this is general clarification. Sorry to ask in this Chain.

    I have a page which has few links on top,
    if i click on any of the links,
    respective page will open below to the links. But it will not refresh the entire page.

    here i have used the by.id for the webelements but am getting an error.

    Kindly do the needful.

    Thanks in Advance

  10. hi seniors !!!
    when i was run my first selenium webdriver program i get this probem, and i already change the compatibility of java softwadre..

    “Exception in thread “main” java.lang.NoClassDefFoundError: com/google/common/base/Function
    at Webdriver.main(Webdriver.java:9)
    Caused by: java.lang.ClassNotFoundException: com.google.common.base.Function
    at java.net.URLClassLoader$1.run(URLClassLoader.java:299)
    at java.net.URLClassLoader$1.run(URLClassLoader.java:288)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:287)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:399)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:325)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:332)
    … 1 more

    Description Resource Path Location Type
    Build path specifies execution environment JavaSE-1.6. There are no JREs installed in the workspace that are strictly compatible with this environment. WebDriverDemo Build path JRE System Library Problem

  11. import java.util.concurrent.TimeUnit;

    import org.openqa.selenium.By;
    import org.openqa.selenium.Point;
    import org.openqa.selenium.WebDriver;
    import org.openqa.selenium.WebElement;
    import org.openqa.selenium.firefox.FirefoxDriver;
    import org.openqa.selenium.support.ui.Select;
    //import org.openqa.selenium.support.ui.Select;

    //import com.sun.javafx.scene.paint.GradientUtils.Point;

    public class Testing {
    public static void main(String[] args)
    {
    WebDriver driver = new FirefoxDriver();
    driver.get(“https://www.amazon.com”);
    driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
    WebElement image1=driver.findElement(By.xpath(“//span[2]/a/span”));
    image1.click();

    driver.findElement(By.xpath(“//div[@id=’nav-search’]/form/div[2]/div/input”)).click();

    WebElement username = driver.findElement(By.id(“ap_email”));
    username.clear();

    username.sendKeys(“sat.tes111@gmail.com”);

    driver.close();

    }
    }

    can any one help to below error i.e. sendkey is not working

    error occurred in the line “username.sendKeys(“sat.tes111@gmail.com”); ”
    java.lang.CharSequence cannot be resolved. It is indirectly referenced from required .class files” message?

  12. Hi I am working on selenium to validate some website. I have login scenario where 2 invalid attempts need to be done . How to invoke same method 2 times. I mean first time name , password n scenario will fail with error. again give name , password it must fail. Please suggest as I am beginner in this .

  13. you guys are really awesome ..thanks for help us ….great work ..this is the best site tutorial i have ever seen about testing…thank you so much

  14. Exception in thread “main” org.openqa.selenium.WebDriverException: Cannot find firefox binary in PATH. Make sure firefox is installed. OS appears to be: VISTA , me getting this error pls help

  15. The original code of the test gets stuck because google have changed the log in form functionality, but when I tried Raj’s code I get lots of syntax errors, has anyone got the new code working?

  16. I have the following code:

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

    public class Gmail_Login {
    /**
    * @param args
    */
    public static void main(String[] args) {

    // objects and variables instantiation
    WebDriver driver = new FirefoxDriver();
    String appUrl =”https://accounts.google.com”;

    // launch the firefox browser and open
    driver.get(appUrl);

    // maximize the browser window
    driver.manage().window().maximize();

    // declare and initialize the variable to store the expected title of the webpage.
    String expectedTitle = ” Sign in – Google Accounts “;

    // fetch the title of the web page and save it into a string variable
    String actualTitle = driver.getTitle();

    // compare the expected title of the page with the actual title of the page and print the result
    if (expectedTitle.equals(actualTitle))
    {
    System.out.println(“Verification Successful – The correct title is displayed on the web page.”);
    }
    else
    {
    System.out.println(“Verification Failed – An incorrect title is displayed on the web page.”);
    }

    // enter a valid username in the email textbox
    WebElement username = driver.findElement(By.id(“Email”));
    username.clear();
    username.sendKeys(“TestSelenium”);

    // enter a valid password in the password textbox
    WebElement password = driver.findElement(By.id(“Passwd”));
    password.clear();
    password.sendKeys(“password123”);

    // click on the Sign in button
    WebElement SignInButton = driver.findElement(By.id(“signIn”));
    SignInButton.click();

    // close the web browser
    driver.close();
    System.out.println(“Test script executed successfully.”);

    // terminate the program
    System.exit(0);
    }
    }

    BUT GETTING THE FOLLOWING ERROR. WHY?

    Exception in thread “main” java.lang.NoClassDefFoundError: com/google/common/base/Function
    at Gmail_Login.main(Gmail_Login.java:15)
    Caused by: java.lang.ClassNotFoundException: com.google.common.base.Function
    at java.net.URLClassLoader$1.run(Unknown Source)
    at java.net.URLClassLoader$1.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    … 1 more

  17. Hi,

    Wonderful chapter.

    Having one issue.

    Does anyone have a solution?

    (Gmail updated so I had to add a “nxtbtn” string).

    – I run the script
    – Auto-fills ‘username’
    – clicks ‘next’
    – won’t auto-fill ‘password’

    About the time I get to ‘password’, it won’t auto-fill the password.

    I tried all the locator methods, but no luck.

    Here’s the code I’m running:

    WebElement username = driver.findElement(By.id(“Email”));
    username.clear();
    username.sendKeys(“TestSelenium”);

    WebElement nxtbtn = driver.findElement(By.id(“next”));
    nxtbtn.click();

    WebElement password = driver.findElement(By.id(“Passwd”));
    password.sendKeys(“password123”);

    WebElement SignInButton = driver.findElement(By.id(“signIn”));
    SignInButton.click();

    driver.close();
    System.out.println(“Test script executed successfully.”);

    Error code I’m getting:

    Exception in thread “main” org.openqa.selenium.NoSuchElementException: Unable to locate element: {“method”:”id”,”selector”:”Passwd”}

  18. @Raj,
    Thanks Raj, your update of the code works great!!!

    If anyone tries the new code posted from Raj and you have syntax errors, you’d have to retype the quotation marks only e.g. “” then the syntax errors will resolve and then you may run the code.

    Thanks again Raj :))

  19. Raj N,

    How did you figure out to use these codes:

    throws InterruptedException

    System.out.printIn(“title:”+actualTitle);

    Tread.sleep(1000);

    I’d love to know if you could tell us ;)

    Thanks again!!

  20. we use asserts statement,because asserts are used to check weather given condition is correct or not,if its corrects ,it executs the script otherwise ,it will drop the statement.
    synatax:(TestNG–this statement is used for validation)
    Asserts.assert(actualtitle,expectedtitle);

  21. Hi,
    Iam getting the following warning
    Exception in thread “main” org.openqa.selenium.remote.UnreachableBrowserException: Could not start a new session. Possible causes are invalid address of the remote server or browser start-up failure.
    Build info: version: ‘2.53.0’, revision: ’35ae25b’, time: ‘2016-03-15 16:57:40’
    System info: host: ‘Yogesh’, ip: ‘192.168.0.102’, os.name: ‘Windows 10’, os.arch: ‘amd64’, os.version: ‘10.0’, java.version: ‘1.8.0_91’
    Driver info: driver.version: FirefoxDriver
    at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:665)
    at org.openqa.selenium.remote.RemoteWebDriver.startSession(RemoteWebDriver.java:249)
    at org.openqa.selenium.remote.RemoteWebDriver.(RemoteWebDriver.java:131)
    ….

    is it anything to do with the x32 bit x64 bit ???
    thats what i have found on google as of now.
    please help.
    thanks in advance.

  22. I am new to selenium. I just want to know that the program will execute as written like opening gmail, logging in and out.
    How will it show any error or bug at a particular point?

  23. Hi Team,

    I really appreciate your great work to make learners to reach their level 1 in Selenium so Quick.
    and also have my Thank you for the your easy solutions at #42.

    Cheers!!

    Ashok

  24. hey, great post very well done but i am having some issues
    could you take a look

    Error Codes:
    Exception in thread “main” java.lang.IllegalStateException: The path to the driver executable must be set by the webdriver.gecko.driver system property; for more information, see https://github.com/mozilla/geckodriver. The latest version can be downloaded from https://github.com/mozilla/geckodriver/releases
    at com.google.common.base.Preconditions.checkState(Preconditions.java:199)
    at org.openqa.selenium.remote.service.DriverService.findExecutable(DriverService.java:109)
    at org.openqa.selenium.firefox.GeckoDriverService.access$000(GeckoDriverService.java:37)
    at org.openqa.selenium.firefox.GeckoDriverService$Builder.findDefaultExecutable(GeckoDriverService.java:95)
    at org.openqa.selenium.remote.service.DriverService$Builder.build(DriverService.java:296)
    at org.openqa.selenium.firefox.FirefoxDriver.createCommandExecutor(FirefoxDriver.java:265)
    at org.openqa.selenium.firefox.FirefoxDriver.(FirefoxDriver.java:235)
    at org.openqa.selenium.firefox.FirefoxDriver.(FirefoxDriver.java:230)
    at org.openqa.selenium.firefox.FirefoxDriver.(FirefoxDriver.java:226)
    at org.openqa.selenium.firefox.FirefoxDriver.(FirefoxDriver.java:124)
    at Gmail_Login.main(Gmail_Login.java:9)

  25. Hi there,

    Please help! I really enjoy your blog and I’ve just started learning selenium Webdriver but having problems running simple scripts like this on my laptop “Windows 10”:

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

    public class FireFox {

    public static void main(String[] args) {
    // TODO Auto-generated method stub

    WebDriver driver=new FirefoxDriver();

    driver.get(“http//www.google.com”);

    }

    }
    Anytime i run this script, i get this error message in console:

    Exception in thread “main” java.lang.IllegalStateException: The path to the driver executable must be set by the webdriver.gecko.driver system property; for more information, see https://github.com/mozilla/geckodriver. The latest version can be downloaded from https://github.com/mozilla/geckodriver/releases
    at com.google.common.base.Preconditions.checkState(Preconditions.java:199)
    at org.openqa.selenium.remote.service.DriverService.findExecutable(DriverService.java:109)
    at org.openqa.selenium.firefox.GeckoDriverService.access$000(GeckoDriverService.java:37)
    at org.openqa.selenium.firefox.GeckoDriverService$Builder.findDefaultExecutable(GeckoDriverService.java:95)
    at org.openqa.selenium.remote.service.DriverService$Builder.build(DriverService.java:296)
    at org.openqa.selenium.firefox.FirefoxDriver.createCommandExecutor(FirefoxDriver.java:277)
    at org.openqa.selenium.firefox.FirefoxDriver.(FirefoxDriver.java:247)
    at org.openqa.selenium.firefox.FirefoxDriver.(FirefoxDriver.java:242)
    at org.openqa.selenium.firefox.FirefoxDriver.(FirefoxDriver.java:238)
    at org.openqa.selenium.firefox.FirefoxDriver.(FirefoxDriver.java:127)
    at FireFox.main(FireFox.java:9)

    Please help I’m desperate!

    I’m using

    Windows 10
    selenium 3.0.1
    Firefox 50.0.1
    Eclipse Neon.1

    BIG THANKS

  26. @ Franck :
    You need to install gecko driver and after add the line :
    System.setProperty(“webdriver.gecko.driver”, “C:\\junit\\webdriver\\geckodriver.exe”);
    at the begining of the main method with the good path for you for geckodriver.
    So, something like that :

    public static void main(String[] args) {

    System.setProperty(“webdriver.gecko.driver”, “C:\\junit\\webdriver\\geckodriver.exe”);

    // objects and variables instantiation
    WebDriver driver = new FirefoxDriver();

    }

    I hope that help you.

    Best regards,

    Bruno

  27. Create Gmail Account –
    package testcase1;

    import java.util.concurrent.TimeUnit;

    import org.openqa.selenium.By;
    import org.openqa.selenium.WebDriver;
    import org.openqa.selenium.WebElement;
    import org.openqa.selenium.firefox.FirefoxDriver;
    import org.openqa.selenium.support.ui.Select;

    public class Test1 {

    public static void main(String[] args) throws InterruptedException {
    WebDriver driver = new FirefoxDriver();
    driver.get(“https://www.google.co.in/?gfe_rd=cr&ei=d9qWWI7eO7HT8ge-h6DYBA&gws_rd=ssl”);

    driver.findElement(By.linkText(“Gmail”)).click();

    driver.findElement(By.linkText(“Create account”)).click();
    driver.findElement(By.id(“FirstName”)).sendKeys(“Kavya”);
    WebElement TxtBoxContent = driver.findElement(By.id(“errormsg_0_LastName”)); //Object creation of web element.

    TxtBoxContent.getText();
    driver.findElement(By.id(“LastName”)).sendKeys(“Jain”);
    driver.findElement(By.id(“GmailAddress”)).sendKeys(“jainkavya706@gmail.com”);
    driver.findElement(By.id(“Passwd”)).sendKeys(“admin@123”);
    driver.findElement(By.id(“PasswdAgain”)).sendKeys(“admin@123”);

    driver.findElement(By.xpath(“.//*[@id=’BirthMonth’]/div[1]”)).sendKeys(“March”); //Entering BirthMonth

    driver.findElement(By.id(“BirthDay”)).sendKeys(“2”); //Entering BirthDay.
    Thread.sleep(1000);

    driver.findElement(By.id(“BirthYear”)).sendKeys(“2009”); //Entering Birth year.
    Thread.sleep(1000);

    driver.findElement(By.cssSelector(“#Gender > div.goog-inline-block.goog-flat-menu-button.jfk-select”)).sendKeys(“Female”); //Entering Gender.
    Thread.sleep(1000);

    driver.findElement(By.xpath(“.//*[@id=’RecoveryPhoneNumber’]”)).sendKeys(“1234568921”);
    driver.findElement(By.xpath(“.//*[@id=’submitbutton’]”)).click();
    Thread.sleep(1000);
    driver.manage().window().maximize();
    driver.findElement(By.id(“iagreebutton”)).click();
    Thread.sleep(1000);

    }

    }

  28. Hello,

    I have created script for gmail login , Please check below:-

    package sanityTest;

    import java.util.concurrent.TimeUnit;

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

    public class Test2 {

    public static void main(String[] args) {

    WebDriver driver = new FirefoxDriver();
    driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);

    driver.get(“https://mail.google.com/”);
    driver.manage().window().maximize();
    driver.findElement(By.id(“Email”)).sendKeys(“for exa xyz@gmail.com“);
    driver.findElement(By.id(“next”)).click();

    driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);

    driver.findElement(By.name(“Passwd”)).sendKeys(“for example 123456”);
    driver.findElement(By.id (“signIn”)).click();
    driver.close();

    The above script is correct for gmail login but after run it takes email in email field, clicks on “next” button and redirect on next page but password is not entered on this page and also not clicks “SignIn” button. Please help.

  29. Hi. I’m also getting the error unable to locate element: *[name=’Passwd’], but the password textbox has the name ‘Passwd’. I even tried changing this to say to look for ID=’Passwd’ with no luck. Any help on this is greatly appreciated.

  30. Exception in thread “main” java.lang.IllegalStateException: The path to the driver executable must be set by the webdriver.gecko.driver system property; for more information, see https://github.com/mozilla/geckodriver. The latest version can be downloaded from https://github.com/mozilla/geckodriver/releases
    at com.google.common.base.Preconditions.checkState(Preconditions.java:738)
    at org.openqa.selenium.remote.service.DriverService.findExecutable(DriverService.java:124)
    at org.openqa.selenium.firefox.GeckoDriverService.access$100(GeckoDriverService.java:41)
    at org.openqa.selenium.firefox.GeckoDriverService$Builder.findDefaultExecutable(GeckoDriverService.java:115)
    at org.openqa.selenium.remote.service.DriverService$Builder.build(DriverService.java:330)
    at org.openqa.selenium.firefox.FirefoxDriver.toExecutor(FirefoxDriver.java:207)
    at org.openqa.selenium.firefox.FirefoxDriver.(FirefoxDriver.java:108)
    at org.openqa.selenium.firefox.FirefoxDriver.(FirefoxDriver.java:104)
    at selenium.gmail_login.main(gmail_login.java:16)

    When i run this script, then we got above exceptions. please provide me solution for this.

    Thank You!

  31. when i run following script,then i have get following exceptions, please provide me solution for this.

    Exception in thread “main” java.lang.IllegalStateException: The path to the driver executable must be set by the webdriver.gecko.driver system property; for more information, see https://github.com/mozilla/geckodriver. The latest version can be downloaded from https://github.com/mozilla/geckodriver/releases
    at com.google.common.base.Preconditions.checkState(Preconditions.java:738)
    at org.openqa.selenium.remote.service.DriverService.findExecutable(DriverService.java:124)
    at org.openqa.selenium.firefox.GeckoDriverService.access$100(GeckoDriverService.java:41)
    at org.openqa.selenium.firefox.GeckoDriverService$Builder.findDefaultExecutable(GeckoDriverService.java:115)
    at org.openqa.selenium.remote.service.DriverService$Builder.build(DriverService.java:330)
    at org.openqa.selenium.firefox.FirefoxDriver.toExecutor(FirefoxDriver.java:207)
    at org.openqa.selenium.firefox.FirefoxDriver.(FirefoxDriver.java:108)
    at org.openqa.selenium.firefox.FirefoxDriver.(FirefoxDriver.java:104)
    at selenium.gmail_login.main(gmail_login.java:16)

  32. Hi All,
    I have a query on my below program. Please help me to out.

    Am directly passing the username and Password using SendKeys() command. I need to create a IF Else loop to validate this Username/password details.
    For ex: If am giving invalid username/Password,I will give the message in Println statement.

    Program:

    public class bankingproject {

    WebDriver driver;

    @BeforeClass
    public void open_webpage_for_banking_project()
    {
    System.setProperty(“webdriver.chrome.driver”, “D:\\chromedriver.exe”);
    driver=new ChromeDriver();
    driver.get(“http://www.demo.guru99.com/v4/”);
    }

    @Test
    public void validate_login_details()
    {
    String user_name,password;
    driver.findElement(By.cssSelector(“input[type=\”text\”]”)).sendKeys(“UserId”);
    driver.findElement(By.cssSelector(“input[type=\”password\”]”)).sendKeys(“Password”);
    driver.findElement(By.name(“btnLogin”)).click();
    System.out.println(“Login Successful”);

    }

    @AfterClass
    public void close_browser()
    {
    //driver.quit();
    }
    }

  33. i am getting this error while running abouve code..

    Could not find or load main class net.sf.cglib.transform.hook.AsmClassLoaderPreProcessor

  34. Hi, when I finally Ran the test, I got this in my console:

    Exception in thread “main” java.lang.IllegalStateException: The path to the driver executable must be set by the webdriver.gecko.driver system property; for more information, see https://github.com/mozilla/geckodriver. The latest version can be downloaded from https://github.com/mozilla/geckodriver/releases
    at com.google.common.base.Preconditions.checkState(Preconditions.java:738)
    at org.openqa.selenium.remote.service.DriverService.findExecutable(DriverService.java:124)
    at org.openqa.selenium.firefox.GeckoDriverService.access$100(GeckoDriverService.java:41)
    at org.openqa.selenium.firefox.GeckoDriverService$Builder.findDefaultExecutable(GeckoDriverService.java:115)
    at org.openqa.selenium.remote.service.DriverService$Builder.build(DriverService.java:330)
    at org.openqa.selenium.firefox.FirefoxDriver.toExecutor(FirefoxDriver.java:207)
    at org.openqa.selenium.firefox.FirefoxDriver.(FirefoxDriver.java:108)
    at org.openqa.selenium.firefox.FirefoxDriver.(FirefoxDriver.java:104)
    at Gmail_Login.main(Gmail_Login.java:13)

    Please help, I don’t know what I did wrong. Thanks!

  35. Perhaps the ‘copy/paste’ would work better if line numbers where not included. Beats typing it all in, but deleting each line number is a pain. :)

Leave a Comment