Book Image

Learning Selenium Testing Tools - Third Edition

Book Image

Learning Selenium Testing Tools - Third Edition

Overview of this book

Table of Contents (22 chapters)
Learning Selenium Testing Tools Third Edition
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Tips and tricks


In this section, we will take a 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 whether 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 NoSuchElementException, then we know it's not there. Unfortunately, I, and many others, are not big fans of using exception handling as a means 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 should be zero and can be checked accordingly