Time for action – using findElementByLinkText() - Selenium

We are going to find an element on the page by using the findElementByLinkText() method that is on each of the Browser Driver classes.findElement calls will return a WebElement object that we can perform actions on.

Follow these steps to see it work:

  1. Open your Java IDE. Intellij or Eclipse are the most used.

  2. We are going to use the following command. We will use a different page on the site for this example.

    Driver.get("http://book.theautomatedtester.co.uk") WebElement element = ((FindsByLinkText)driver). findElementByLinkText("Chapter1");
  3. Run the test from the IDE. It will look like the following screenshot:

What just happened?

We have just seen how we can find an element by using the findElementByLinkText helper method. One thing to note is that the search for the text is case sensitive when used in WebDriver. This means that what we pass into Selenium, WebDriver needs to match exactly or it will not find your element. If an element is not found in Selenium, WebDriver will throw a NoSuchElementFound exception.

Finding elements on the page by their link text

We will now see how we can find elements on the page by their link text in action. The method's signature looks like:

findElementsByLinkText(String using);

The using variable takes the link text of the element that you wish to look for. It will return a WebElement object that we can then work with.

All rights reserved © 2018 Wisdom IT Services India Pvt. Ltd DMCA.com Protection Status

Selenium Topics