Finding Text Object in Selenium

211 views
Skip to first unread message

Selenium007

unread,
Jun 22, 2018, 12:19:58 PM6/22/18
to Selenium Users
<table class="questionKeyHeader">
   <tbody>
      <tr valign="top">
         <td>
            <a>A0310C</a> = 0<br>
            <a>A2300</a> = 05/17/2018<br>
            <a>A2400B</a> = 05/12/2018<br>
            <a>A2400C</a> = --/--/----<br>
            <a>H0200C</a> = Blank/Skipped<br>
            <a>H0500</a> = 0<br>
            <a>O0100E2</a> = 0<br>
            <a>O0100F2</a> = 0<br>
            <a>O0100M2</a> = 0<br>
         </td>
      </tr>
   </tbody>
</table>
Above is the HTML this Selenium test is trying to parse. The goal is to find the text that comes after the element. The following xpath will find a given element:
.//following::table[@class="questionKeyHeader"]//td/a[text()="A2300"]

<table class="questionKeyHeader">
   <tbody>
      <tr valign="top">
         <td>
            <a>A0310C</a> = 0<br>
            <a>A2300</a> = 05/17/2018<br>
            <a>A2400B</a> = 05/12/2018<br>
            <a>A2400C</a> = --/--/----<br>
            <a>H0200C</a> = Blank/Skipped<br>
            <a>H0500</a> = 0<br>
            <a>O0100E2</a> = 0<br>
            <a>O0100F2</a> = 0<br>
            <a>O0100M2</a> = 0<br>
         </td>
      </tr>
   </tbody>
</table>

Above is the HTML this Selenium test is trying to parse. The goal is to find the text that comes after the element. The following xpath will find a given element:

.//following::table[@class="questionKeyHeader"]//td/a[text()="A2300"]

But it fails when appending /following-sibling::text() to the end of it. What is the trick to get at the text between the < a > and < br >?

total QA

unread,
Jun 25, 2018, 4:24:50 AM6/25/18
to Selenium Users
Hi,

You can use Java script executor as well. Then it might helps you.

//find the Element
String text = (String)((JavascriptExecutor)driver).executeScript("return arguments[0].nextSibling.textContent.trim()", element);


Serguei Kouzmine

unread,
Jul 21, 2018, 11:51:05 AM7/21/18
to Selenium Users
Interesting , while as correctly suggested by total-qa, the
DOM  method works nicely, 
the Selenium  method driver.findElements(By.xpath when fed with either of 

"following-sibling::text()" and "following-sibling::node()"
receives
org.openqa.selenium.InvalidSelectorException:
invalid selector:
The result of the xpath expression "following-sibling::node()" is: [object Text]. It should be an element.

is it a bug in Selenium ? 

Thanks,

Serguei Kouzimine

Jim Evans

unread,
Jul 21, 2018, 3:43:57 PM7/21/18
to Selenium Users
No, it is not a bug in Selenium. You cannot select a text node in Selenium using XPath. Even though such an XPath query would be valid on an XML file (for use in an XSLT transform, for instance), HTML/DOM is not equivalent to XML.

A brief history lesson: XPath was not intended to be an element location strategy for HTML documents. It was intended to be used for XML documents. While there are some similarities between HTML and XML (both are roughly hierarchical in structure), they are not identical. HTML in particular is much looser in its allowed markup formatting than the much stricter XML. Therefore, when using Selenium WebDriver’s findElement using an XPath locator, the result must resolve to an element, not any other type of (otherwise valid) XPath node type, like an attribute node or a text node.
Reply all
Reply to author
Forward
0 new messages