WebDriver tricks of the trade

A post to be used as a reference for items/issues that has surfaced when using WebDriver with Java.


Muting WebDriver logs

At points where WebDriver is really verbose it is quite convenient to mute the output or at least certain levels of it.

// Java
// Set it before creating the WebDriver instance to avoid having any messages generated from the
// constructor call
RemoteWebDriver.setLogLevel(Level.WARNING); // java.util.logging.Level
WebDriver driver = new FirefoxDriver(profile);


Avoiding untrusted SSL cerificate errors when using Firefox

Typical test environment issue. A simple workaround that worked for us was to simply set the profile used to accept and assume any certificate issues.

// Java
FirefoxProfile profile = new FirefoxProfile();
WebDriver driver = new FirefoxDriver(profile);


Running FireFox in full screen

Especially handy when combining Sikuli and WebDriver, you need to be absolutely sure that as much as possible is visible on the screen.



Relative xpaths from the current WebElement

Very useful when working with complex HTML-pages whose elements are very deep down and can only be found using xpath. Maintaining these long xpaths in multiple places in the test code base is a pain. The solution is to do the xpathing in parts to minimize the effort needed to maintain the xpath-strings.

		  <td>cell to find</td><td></td>

Starting the xpath-string with the DOT is equivalent to starting the search from the current node.

WebElement tableElement = driver.findElement(By.xpath("//html/body/table"));
// Using relative xpath from the beginning of the table to find the first cell.
// Note that it has to start with the '.', DOT
WebElement cell = driver.findElement(By.xpath("./tbody/tr[1]/td[1]"));


Switching between open windows

Based on words in the page title.

Set<String> windows = driver.getWindowHandles();
for (String window : windows) {
  if (driver.getTitle().contains(pageTitle)) {


Dismissing/accepting alert dialog windows

Whenever an alert window pops-up it has to be accepted or dismissed to avoid blocking the test execution, this is simply handled using Alert class (Selenium docs).

// WebDriver driver = new FirefoxDriver();
Alert alert = driver.switchTo().alert();
// OR
// alert.dismiss();