O'Reilly logo

Selenium 2 Testing Tools Beginner's Guide by David Burns

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

Tips and tricks

In this section, we are going to look at some tips and tricks that might be of use when trying to find elements on the page. We can also apply them to see if the elements are not on the page.

Finding if an element exists without throwing an error

Selenium WebDriver is really good at letting you know when an element does not exist. If it throws a NoSuchElementException, then we know it's not there. Unfortunately I, and many others, have not been big fans of using exception handling as a way of flow control.

To get around this we can use the findElements() call, and then we just need to check that the size of the list returned is 0. For example:

List<WebElement> elements = driver.findElements(By.Id("myElement")); elements.size(); //This ...

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