How to – Selenium WebDriver 2.35 in Java on Windows 7 with no IDE to test all browsers

Posted by & filed under .


I just recently used Selenium WebDriver and I found out that the documentation of this software is missing a lot of information and there’s clearly not enough examples so that one can get started rapidly. After a few hours browsing the web and a lot of testing, I finally made a working script. In the end, Selenium is quite powerful and can do a lot, the learning curve is just a bit steep. Here’s some information on how to run Selenium with code examples so you can make your own script. Like the title says, it is coded in Java on Windows 7, the java code would be the same on other platform but the installation is a bit different if you want to run it on Linux for example but they can be modified easily.

Before I start with the installation, note that the Selenium-IDE for Firefox only is easy and user-friendly to use, the only problem is that it will test Firefox only, so continue reading if you also want to test more browsers.

First thing is installing the Java Development Kit (aka JDK). Then download Selenium, put it in C:\selenium and extract it there, it will create a folder selenium-java-2.35.0 and inside there will be a folder libs like so: C:\selenium\selenium-java-2.35.0\libs

Install Firefox, Chrome and Internet Explorer if you want to use Selenium to test all browsers and if you do, also download the Internet Explorer Driver Server and the Chrome Driver for Selenium, I’m using IEDriverServer_Win32_2.35.3 and chromedriver_win32_2.3 (both can be found on the Selenium web page). Note that the driver for IE is also available in 64 bits but beware of it because it runs very slowly. Extract them in C:\selenium

There’s always something about Internet Explorer and this time is no different, you must set the Protected Mode in Options for each zone to be the exact same value, doesn’t matter if it’s off or on, just make sure it’s the same everywhere. And you must set the zoom level at 100%, it is very important.

Create a file named in C:\selenium\test to make it look like: C:\selenium\test\

Open up a command console, copy and paste those lines below, make sure that all paths are existing:

set path=%path%;C:\Program Files\Java\jdk1.7.0_40\bin
set path=%path%;C:\selenium\IEDriverServer_Win32_2.35.3
set path=%path%;C:\selenium\chromedriver_win32_2.3
set classpath=C:\selenium\test
set classpath=%classpath%;C:\selenium\selenium-java-2.35.0\selenium-java-2.35.0.jar
set classpath=%classpath%;C:\selenium\selenium-java-2.35.0\selenium-java-2.35.0-srcs.jar
set classpath=%classpath%;C:\selenium\selenium-java-2.35.0\libs\*
cd C:\selenium\test

Even though the file is empty for now, this is how you will compile it and run it:

cmd /c call javac & java login

And here’s the whole code, it doesn’t do anything but keep on reading for what to do next!

import java.util.List;
import java.util.Set;

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.Capabilities;
import org.openqa.selenium.HasCapabilities;
import org.openqa.selenium.remote.RemoteWebDriver;
import org.openqa.selenium.WebElement;

public class login {
public static int TheBrowser = 3; // 1=Firefox, 2=IE, 3=Chrome
public static int WaitTime = 1000;

public static String msg;
public static String browserName;
public static WebDriver driver;
public static WebElement input;
public static Select select;

public static void main(String[] args) {
if ( TheBrowser == 1 ) {
	driver = new FirefoxDriver();
else if ( TheBrowser == 2 ) {
	InternetExplorerDriverService service = new InternetExplorerDriverService.Builder().withLogFile(new File("C:\\selenium\\iedriver.log")).withLogLevel(InternetExplorerDriverLogLevel.TRACE).build();
	driver = new InternetExplorerDriver(service);
else if ( TheBrowser == 3 ) {
	driver = new ChromeDriver();

// add a delay of 10 seconds in case the webpage is still loading and Selenium doesn't find what you're looking for
WebDriverWait wait = new WebDriverWait(driver, 10);

// Set the Capabilities, it provides many useful information, in this case it's the browser name
Capabilities caps = ((RemoteWebDriver) driver).getCapabilities();
browserName = caps.getBrowserName();

// Get the page where you want to go first

// Store the Window handle in the variable MainWindow, that is in case your script open up another window, we can always come back to the first window
String MainWindow = driver.getWindowHandle(); 

// Maximize the browser window

} //end of void main

// Sometimes you need to refresh driver.get after you change a page, like after you click a Login button
// there's also an exception for IE, it needs to wait a bit more
public static void ChangePage() {
	if ( login.browserName.equals("internet explorer") ) {
		try { Thread.sleep(2000); } catch (InterruptedException ex) { Thread.currentThread().interrupt(); }

} //end of Class

When testing, you will want to comment out driver.quit(); so that the script doesn’t close the window. So you can now compile and run it but it will really do nothing! Here’s what you can do now, one of the most useful feature would be to click on something, this code will click on something where the CSS’s ID is “button”:


The wait.until function is related to the WebDriverWait, it will try to find the element for up to 10 seconds before exiting. If what you want to click has no ID but has a CSS’s Class, try this:


Or if it’s a link, you can click on it by it’s text:

wait.until(ExpectedConditions.presenceOfElementLocated(By.linkText("Click on me!"))).click();

If it’s sharing a class with another button and it’s the second button:


This one use xpath, supposedly it is not well recommended because it might not work with Internet Explorer from time to time, I personally didn’t use it much but it did work on all browsers. This example will click on the first button of the first div tag of the third div of the fourth div in the body tag which is in the html tag, sounds complicated but it just follows the hierarchy of all the tag on the webpage. You can understand this one should only be used as a last recourse because if the webpage changes, like if there’s a new div or one is removed, this will not work anymore, it is better to rely on ID and Class.


There’s a lot of way to find elements in Selenium and sometimes a cssSelector which should be working will not work, keep modifiying it until Selenium accept it. A neat trick is using Selenium-IDE for Firefox to create a script and see what code it produce, then you can just use the same code. For example, I wanted my script to click on an image which had a Class with the function by.className but couldn’t make it work. I tried in Selenium-IDE and this is what it gave me which worked: By.cssSelector(“img[alt=’The Image’]”)

After the click, here’s how you can fill out an input box, in this example the input box name tag is Login:

input = wait.until(ExpectedConditions.presenceOfElementLocated("Login")));

Print text to the command console screen:

System.out.println("\n** This is a test with " + browserName + " **\n");

Get text on the webpage in the ID “content” and print it in the command console screen:

msg = wait.until(ExpectedConditions.presenceOfElementLocated("content"))).getText();

If clicking on an element in a webpage will activate some javascript or jQuery code which will display something after a delay, you must also add the same delay in your code, this will delay for 2 seconds:

try { Thread.sleep(2000); } catch (InterruptedException ex) { Thread.currentThread().interrupt(); }

Test if there’s an element on the webpage:

if ( driver.findElements(By.cssSelector("")).size() > 0 ) {
// element found!

Select the element “Linux FTW!” in a dropdown menu which has an ID “main_dropdown”:

select = new Select(wait.until(ExpectedConditions.presenceOfElementLocated("main_dropdown")) ));
select.selectByVisibleText("Linux FTW!");

There’s a lot more with frames, windows and so on but all of this should help a lot and if you don’t like coding in a text file, just give Maven a try, it’s an IDE that supposedly works quite well for coding Selenium. Also of help is the official Selenium WebDriver page and the complete list of all the Selenium java packages.

Leave a Reply

Your email address will not be published. Required fields are marked *