Getting started with Selenium WebDriver in Java (Windows)

[renewed at 5 Dec 2018]
IMHO Java is the most popular programming language in the Selenium project, which means, that whenever you face a Selenium problem, the probability of finding the solution in Java is the highest. So start using Selenium in Java!
There are generelly 3 approches to get 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 takes the most time but is the most back-to-the-roots one and shows best how Java works under the hood.

Approach without IDE, without Maven

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).
Selenium Setup
Download Selenium Standalone Server and put it e.g. on your desktop.
Selenium on Chrome works pretty out of the box: Download ChromeDriver, unzip it to e.g. the desktop and just run it.
If you want to run your tests on Internet Explorer, you need more time for configuration: look
Your first Selenium program
1.) Copy the following code in an editor and save it as e.g. on your Desktop:

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
public class Example  {
    public static void main(String[] args) {
        // Create a new instance of the Chrome driver
        // Notice that the remainder of the code relies on the interface,
        // not the implementation.
        WebDriver driver = new ChromeDriver();
        // 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());

2.) As every Java program, it needs to be compiled. For the Java Compiler to find your Selenium library, you have to provide it in the so-called classpath. Execute the following in your Command Prompt (of course with the correct version number):

javac -classpath "%UserProfile%\Desktop\selenium-server-standalone-3.141.59.jar" %UserProfile%\Desktop\

3.) If everything is ok, you find Example.class on your desktop. Execute the following in your Command Prompt to run your first Selenium program (of course with the correct version number):

cd "%UserProfile%\Desktop

java -classpath .;"%UserProfile%\Desktop\selenium-server-standalone-3.141.59.jar" Example
… for historical reasons the JRE needs to point also to the actual folder, that is .;
Now Chrome should 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 Gedanken zu „Getting started with Selenium WebDriver in Java (Windows)“

  1. 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.

    1. 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. 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.

Kommentare sind geschlossen.