Getting started with Selenium WebDriver in Java on Windows


14. January 2013 by Michael Wowro

My personal opinion is, that Java is the most popular programming language in the Selenium project.
There are generelly three approches to getting started with Selenium in Java:

The official one: Setting Up a Selenium-WebDriver Project
The sophisticated one (by Mark Collin, aka Ardesco): Selenium-Maven-Template

The official one: The 5 Minute Getting Started Guide Note: Here you find an example with HtmlUnitDriver. I experienced too often, that HtmlUnitDriver wasn’t working even with correct Selenium code, so I recommand not using it at all.

without IDE, without Maven
This approach is, regarding Java, the most “back-to-the-roots” one – but takes the most time. I’ll show this approach in this article. If you like to start with Selenium while refreshing you Java knowledge – take this one.

All guides are different ways for the same goal: get your first Selenium program run.

Some backgrounds to Java
This paragraph is only a little refreshment for your aged Java knowledge. If you never had Java knowledge, you should go for a tutorial.
If you haven’t done yet, download the Java JDK from the download page (take the most actual Java Platform (JDK)). Execute it and follow the wizard. After that, set PATH.
The Java JDK not only comprises the JRE (jre.exe) – the JRE is the environment to run java-programs – but also the Java Compiler (javac.exe). The Compiler is necessary, because you have to compile the file of your source-code ( to become an executable file. The result of that compilation is yourname.class. The program then can be executed by java yourname in your Command Prompt (after you navigated in the directory, where the yourname.class resides).

To run your first Selenium program on Firefox, you need to download Firefox here and install it.

Selenium Setup
For working with Selenium, you need to include the right Selenium library. There are plenty of libraries available in the Selenium project, which probably is confusing in the beginning: download page. A good choice is the latest version of selenium-server-standalone-x.y.z.jar (x, y and z will be digits e.g. 2.28.0 at the time of writing) – this library contains everything you need for your first steps in Selenium. Download and copy it to a folder of your choice. That’s it – so easy!

Your first Selenium program

Copy the following code in an editor and save it as

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

public class Example  {
    public static void main(String[] args) {
        // Create a new instance of the Firefox driver
        // Notice that the remainder of the code relies on the interface, 
        // not the implementation.
        WebDriver driver = new FirefoxDriver();

        // And now use this to visit Google

        // Find the text input element by its name
        WebElement element = driver.findElement("q"));

        // Enter something to search for

        // Now submit the form. WebDriver will find the form for us from the element

        // Check the title of the page
        System.out.println("Page title is: " + driver.getTitle());

As this Selenium program is just a normal Java program, you have to compile the sourcecode-file. For the Java Compiler to find your Selenium library, you have to provide it in the so-called classpath. This is the composition of the classpath:
1.) quote
2.) the path to the folder where you put in the selenium-server-standalone-x.y.z.jar.
3.) a backslash
4.) selenium-server-standalone-x.y.z.jar
5.) quote

So execute the following in your Command Prompt:

javac -classpath "pathToJAR\selenium-server-standalone-x.y.z.jar"

If everything is ok, you find Example.class in your folder. Your first Selenium program is executable now and you run it with executing the following in the Command Prompt:

java -classpath .;"pathToJAR\selenium-server-standalone-x.y.z.jar" Example

[Unimportant background: For historical reasons the JRE needs to point also to the actual folder, that is .;]

Now Firefox should start, write “Cheese!” in google search and finally you should find in the Command Prompt: “Page title is: Google”. The browser-window is not closing, as you might admire the results for some time!


6 thoughts on “Getting started with Selenium WebDriver in Java on Windows

  1. Kalpana J says:

    I ran sample program to get cheese title using above mentioned steps but getting below error-
    “Caused by: org.openqa.selenium.firefox.NotConnectedException: Unable to connect to host on port 7055 after 45000 ms. Firefox console output:”
    please help me on this to resolve.

    • Welcome Kalpana,

      seems that your Firefix version and selenium-standalone-server version don’t match. A good rule of thumb is to choose the newest version for each.

      Yours Michael

  2. Karishma Sundaram says:

    First off, thank you for a such an easy to follow tutorial. I haven’t coded in a long time, and I find myself needing to understand how Selenium works out of the blue.

    I managed to get to the compilation part successfully. After that, once I try to execute the class file, I get the following error:
    Error: Could not find or load main class JavaSample.

    (My class file is Java Sample)

    The instruction I used for execution is the same as the compile instruction, except it is java instead of javac.

    I would really appreciate any help you can give me.

    Thanks in advance.

  3. Harika says:

    Please find the below useful link which provides selenium webdriver tutorials .which will also guide you to build a good framework.

  4. nabhanya says:

    This is a very Good tutorial for selenium webdriver with java . Has many examples to start and work with.

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 )

Twitter picture

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

Facebook photo

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

Google+ photo

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

Connecting to %s

%d bloggers like this: