1

I have a table with data. I need to check that table contains some data and does not contain any other data. So I need to check that table contains entries equal to some unique value.

I solved that task in two steps:

The first step is using XPath function contains() which check that table contains interesting elements.

Second step checks that table does not have other elements which are not equal search value by using XPath not contains() function.

Example:

 List <WebElement> rows = tableBody.findElements(By.xpath("//*[not(contains(td,"+ elementName + "))]"));

This solution is not good, because not contains() function runs twice longer.

Is there other solution how to check that table has elements with search pattern and contains only them?

| improve this question | | | | |
  • I think this would more optimize and correct way. It is my opinion from exp. – Sagar007 Jul 4 '17 at 9:56
  • made title reflect comments – Michael Durrant Jul 4 '17 at 11:10
  • You can refer to this link on stack overflow. Here they are discussing the same thing as mentioned in your question. – Aalok Jul 4 '17 at 12:14
3

When you call .findElement(By.xpath(...)) on an element, and your expression starts with // - webdriver would actually traverse the whole HTML tree from the very root element. If you want it to be executed in the context of the element you call findElement on - prepend a dot: .//.

Alternative solution

If you are looking for speed, one out-of-the-box approach would be to get the table element source HTML and use an HTML parser (e.g. jsoup) to apply your requirements. This has multiple advantages:

  • you are not issuing any extra WebDriver JSON over HTTP calls, no overhead
  • you are not traversing the whole tree with XPaths - you are working in the scope of your table

To get the HTML source code of the element, use getAttribute():

tableBody.getAttribute("innerHTML");
| improve this answer | | | | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.