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:
    driver.get("http://book.theautomatedtester.co.uk")
    driver.findElement(By.linkText("Chapter1"));
  3. Run your ...

Get Learning Selenium Testing Tools - Third Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.