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:
Open your Java IDE.
Use the following command:
driver.get("http://book.theautomatedtester.co.uk") driver.findElement(By.linkText("Chapter1"));
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...