O'Reilly logo

Selenium Testing Tools Cookbook by Unmesh Gundecha

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Checking an element's presence

The Selenium WebDriver doesn't implement Selenium RC's isElementPresent() method for checking if an element is present on a page. This method is useful for building a reliable test where you can check an element's presence before performing any action on it.

In this recipe, we will write a method similar to the isElementPresent() method.

How to do it...

For implementing the isElementPresent() method, follow these steps:

  1. Create a method isElementPresent() and keep it accessible to your tests as follows:
    private boolean isElementPresent(By by) {
        try {
            driver.findElement(by);
            return true;
        } catch (NoSuchElementException e) {
            return false;
        }
    }
  2. Implement a test which calls the isElementPresent() method. It will check if the ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required