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
About the Author
About the Reviewers

Finding elements using a more generic method

We have had a look at using helper methods to find elements on the page. The downside to this is that, if something changes, you need to change the entire method that you are using to find the element. This can increase maintenance costs.

The other approach is to use the findElement() method, pass in the By abstract class, and call static methods on this class.

Let's see this in action.

Using findElement()

In this section, we will look at using the findElement call that is on the WebDriver object. This is how we normally find elements using Selenium WebDriver:

  1. Open your Java IDE.

  2. Use the following command:

  3. Run your test.

We have just seen that we can find an element by passing in the By object. This is a static class that gives people a mechanism to find elements, as we did earlier in the chapter. This will throw NoSuchElementException if it cannot find the...