Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can Selenium evaluate all XPath elements?

Tags:

selenium

xpath

Is it possible for Selenium to evaluate all the elements that may match a certain XPath?

For instance, I'd like to evaluate whether all checkboxes are checked with //input[type='checkbox']--problem is I only get one element returned.

like image 448
Filip Dupanović Avatar asked Sep 10 '09 13:09

Filip Dupanović


People also ask

Can we use full XPath in Selenium?

XPath in Selenium is a technique that allows you to navigate the structure of a webpage's HTML. XPath is a syntax for finding elements on web pages. Using UXPath in Selenium helps find elements that are not found by locators such as ID, class, or name. XPath in Selenium can be used on both HTML and XML documents.

How many types of XPath are there in Selenium?

XPath can be used to locate any element on a page based on its tag name, ID, CSS class, and so on. There are two types of XPath in Selenium.

Is XPath always available for an element?

It can search elements anywhere on the webpage, means no need to write a long xpath and you can start from the middle of HTML DOM structure. Relative Xpath is always preferred as it is not a complete path from the root element.


2 Answers

You can use the getXpathCount command to determine the number of matching elements. You can then loop through them using an increment to locate each element individually. The following Java (TestNG/JUnit) example would check that all checkboxes on a page are checked:

int totalCheckboxes = session().getXpathCount("//input[@type='checkbox']").intValue();
for (int i = 1; i < totalCheckboxes+1; i++) {
    assertTrue(session().isChecked("//input[@type='checkbox'][" + i + "]"));
}
like image 174
Dave Hunt Avatar answered Oct 22 '22 04:10

Dave Hunt


I tried the above approach and selenium was throwing exceptions that element is not found.Adding prefix xpath= resolved the problem.

example

xpath=(//td[@class='cell name bold'])[1]
xpath=(//td[@class='cell name bold'])[2] 
like image 40
samaitra Avatar answered Oct 22 '22 03:10

samaitra