Getting started with Selenium WebDriver in Java on Windows

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!