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.
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.
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.
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.
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 + "]"));
}
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]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With