2

I have xPath which returns boolean:true instead of finding an element on the page.

Test page has a table with 2 columns and multiple rows. I'm testing if the rows have correct values. Below is the sample xPath where I'm checking the values for a given row.

//tr[@class='af_table_data-row']//span[text()='To be Reviewed'] and //span[text()='1']

In firebug, this returns "boolean:true".

I'm lost on how can I read in Selenium. If I try to find as an element and then cast to boolean, it shows error as inconvertible types.

1
  • Please provide what you have tried
    – the_coder
    Commented Apr 25, 2017 at 4:10

2 Answers 2

1

Consider the HTML below. The XPath in your question is checking whether there exists a tr-element in your HTML anywhere (because of the //), with the class 'af_data_table-row', that contains a span anywhere in the tr-element with text 'To be Reviewed' AND a span anywhere in the tr-element that contains the text '1'. Such an element exists, thus the value returned is true. By combining the the two span elements your XPath query with the and its result becomes a boolean result.

If you just want to get the two spans and check their values individually you could use the XPath: //tr[@class='af_table_data-row']/td/span. That will return an array with the 2 span elements. You can then write 2 separate checks to check the values.

However, consider that your original XPath already does enforce that the correct values are in the spans, so by checking for true as a return value from that it also does the check you are looking for, albeit in a less verbose way.

<table>
  <tr class="af_table_data-row"> 
    <td><span>To be Reviewed</span></td>
    <td><span>1</span></td>
  </tr>
</table>
-1

Figured out the solution. You have to use JavascriptExecutor to read the value. Below is the sample code in case if anybody else encounter the same issue.

WebDriver driver = WebDriverFactory.getDriver();
JavascriptExecutor executor = (JavascriptExecutor)driver;
return (Boolean) executor.executeScript("return document.evaluate(\"//tr[@class='af_table_data-row']//span[text()='To be Reviewed'] and //span[text()='1']\", document, null, XPathResult.BOOLEAN_TYPE, null).booleanValue");
1
  • I much prefer @jpjwolli's solution and reasoning Commented May 26, 2017 at 0:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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