Getting started with WebDriver: A simple example

To start with, WebDriver is a tool that’s pretty useful in automating the testing of web apps. As of writing this post, Selenium, another popular and well established testing framework, integrated WebDriver API into Selenium v1 in its Selenium v2.x release.

If you’d like to read more about WebDriver and Selenium, click here.

What do you have to do to see WebDriver in action?
1. Download Selenium v2 Java Bindings ( from here.
2. Create a new Java Project in Eclipse IDE (say, SeleniumWebDriverTestProj).
3. Create a new folder under this project and name it libs (or whatever you think of).
4. Copy\selenium-2.0b3\selenium-java-2.0b3.jar to libs.
5. Copy all jars from\selenium-2.0b3\libs to libs directory.
6. Select all dependencies in libs, right-click on them, and select Build Path > Add to Build Path. Now, you can see some 26 jars added to Referenced Libraries.
7. Create a new class under src directory and name it as you like, say WebDriverTestClass. Add the below code to this class.


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

//import org.openqa.selenium.htmlunit.HtmlUnitDriver;

public class WebDriverTestClass {
	public static void main(String a[]) throws InterruptedException {
		 * Firefox driver is cool, it supports javascript and offers a lot of
		 * features You can also use the below drivers WebDriver driver = new
		 * InternetExplorerDriver(); WebDriver driver = new HtmlUnitDriver();

		WebDriver driver = new FirefoxDriver();

		try {
			// Go to Google Home Page

			// Look for search textbox and enter search term there
			WebElement searchBox = driver.findElement("q"));
			searchBox.sendKeys("WebDriver API");

			// Click on 'Search'
			WebElement searchButton = driver.findElement("btnG"));;

			// Not required or recommended any where, but just wait for the last
			// click()
			// operation to get completed fine

			System.out.println("What's the current Url: "
					+ driver.getCurrentUrl());

			// if you wish to take screenshot of this page, you can!
			File scrFile = ((TakesScreenshot) driver)
			FileUtils.copyFile(scrFile, new File(

			// Close the driver, once you're done.
		} catch (Exception e) {
			e.printStackTrace(); // For debugging purposes



That’s it. Our first example is complete.



  1. Sing,

    Was having a problem with IE Driver, so went out on the web looking for examples. I grabbed yours just to double check my code, and sure enough I am getting the same error.

    org.openqa.selenium.NoSuchElementException: Unable to find element with name == q (WARNING: The server did not provide any stacktrace information)
    System info: ‘Windows 7’, os.arch: ‘x86’, os.version: ‘6.1’, java.version: ‘1.6.0_20’

    Is this running correctly for you?

    only change I made was swapping the InternetExplorerDriver in place of FirefoxDriver.

  2. Hey PMAC,

    This program runs just fine in my laptop (with the following configuration: Windows Vista, x86, IE v8.0, jdk1.6), and I get the expected output. So, I doubt if the page loads fine in your case. You may get this exception, if the expected page doesn’t load fully, and WebDriver still looks for the textbox or any other element in the page.

    I’d recommend putting Thread.sleep(5000) after click() or get() to observe if the page loads fine, and to see what you’ve written works exactly the way you wanted. During debugging, comment out driver.close(); so that it doesn’t get closed prematurely.

    If you still can’t identify, try out the REQUIRED CONFIGURATION mentioned here:


  3. Hi
    I want to retrieve the list of all available buttons and text boxes in a webpage none of whose ids or class names are known in advance. And then, i have to highlight all of them and number them in a serial order.
    All this is to be done using java webdriver.
    Please help me out with this.


    1. Hi Dnyanesh,

      Why don’t you try out searching for textboxes and buttons in the page using CSS selectors? WebDriver supports that.

      Check out the WebDriver API you’re using and see if you use something like driver.findElementsByCssSelector(TEXTBOX_CSS_SELECTOR) or driver.findElements(By.cssSelector(TEXTBOX_CSS_SELECTOR) that’d return you a list of all matching elements.

      About the CSS selector, input[type=text] will list you all text boxes, and input[type=button] will get you buttons.


  4. Hi
    Nice Post…
    I need to study about entire thing about selenium webdriver and its methods and functionalitys…I am beginner… So plz help me to study about Selenium Web driver

  5. Firooz Yazdi · · Reply

    Followed your instructions and got this compilation error;Exception in thread “main” org.openqa.selenium.WebDriverException: Unable to bind to locking port 7054 within 45000 ms
    Build info: version: ‘2.20.0’, revision: ‘16008’, time: ‘2012-02-27 19:03:59’
    System info: ‘Windows 7’, os.arch: ‘x86’, os.version: ‘6.1’, java.version: ‘1.6.0_33’
    Driver info: driver.version: FirefoxDriver………any idea..???

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: