Finding Text Object in Selenium

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

Finding Text Object in Selenium

Selenium007
<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 >?

--
You received this message because you are subscribed to the Google Groups "Selenium Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To post to this group, send email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/selenium-users/87283f2d-bd47-4236-b97f-4c9c05ccff53%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: Finding Text Object in Selenium

total QA
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);



On Friday, June 22, 2018 at 9:49:58 PM UTC+5:30, Selenium007 wrote:
<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 >?

--
You received this message because you are subscribed to the Google Groups "Selenium Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To post to this group, send email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/selenium-users/cafc00f2-250c-46f4-b238-17eddfa951a0%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: Finding Text Object in Selenium

Serguei Kouzmine
In reply to this post by Selenium007
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

On Friday, June 22, 2018 at 12:19:58 PM UTC-4, Selenium007 wrote:
<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 >?

--
You received this message because you are subscribed to the Google Groups "Selenium Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To post to this group, send email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/selenium-users/a0fe86ec-1657-456d-8689-b504bdbcda2f%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: Finding Text Object in Selenium

Jim Evans
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.

--
You received this message because you are subscribed to the Google Groups "Selenium Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To post to this group, send email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/selenium-users/4f1a58cc-5092-428b-b63c-470357190e54%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.