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

Time for action – using findElement()

In this section we are going to look at using the findElement call that is on the WebDriver object. This is how we can normally find elements using Selenium WebDriver.

  1. Open your Java IDE. Intellij or Eclipse are the most used that you could use.
  2. We are going to use the command:
    driver.get("http://book.theautomatedtester.co.uk")
    driver.findElement(By.linkText("Chapter1"));
  3. Run your test.

What just happened?

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 for finding elements, as we did earlier in the chapter. This will throw a NoSuchElementException if it cannot find the element.

Let's now have a look at finding multiple elements.

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