Time for action – using findElementsByXpath() - Selenium

We are going to find an element on the page by using the findElementsByXPath() 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 command:

    List<WebElement> elements = ((FindsByXPath)driver). findElementsByXpath("//input"); Assert.equals(5, elements.size());
  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 findElementsByXPath helper method. We saw that from running the test we saw it pass. Unlike its singular version, it will not throw a NoSuchElement exception if the element is not found. It will return a list that has a size of zero.

Finding an element on the page by its link text

If you need to find a link by the text that is in it, this method is useful. It is a helper method that sets an argument for a more generic findElement call. We will now see how we can use it in action. The method's signature looks like:

findElementByLinkText(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