0
<tr>
 <td align="left" width="200">
  <p>Document Uploaded:&nbsp;Yes</p>
 </td>
</tr>

I'm unable to locate the element with &nbsp; in the text. The below XPath expression does not not work and I've tried so many other suggestions online but have not been successful yet. FYI: I need the entire text not just a substring of it.

//p[contains(text(), 'Document Uploaded: Yes')]
Dimitre Novatchev
  • 240,661
  • 26
  • 293
  • 431
  • Does this answer your question? [Using XPATH to search text containing  ](https://stackoverflow.com/questions/247135/using-xpath-to-search-text-containing-nbsp) – Nowhere Man Oct 06 '20 at 19:35
  • I tried that as well and it did not work @AlexRudenko – automation_tester Oct 06 '20 at 19:45
  • try //*[translate(.,'\u00A0',' ')='Document Uploaded: Yes'] – Millie Anne Volante Oct 07 '20 at 02:48
  • Have you provided the complete XML document or just a fragment of it? Please, provide a complete, but minimal in length document that still exhibits the reported problem. I suspect that the failure to select the wanted element may be due to namespace issues. – Dimitre Novatchev Oct 08 '20 at 00:11
  • Can you, please explain what do you mean by this: "FYI: I need the entire text not just a snippet of it."? The provided XPath expression selects one (in your case) or more `

    ` elements, not text-nodes. Also, if you are specifying in the predicate the string `"'Document Uploaded: Yes'"`, then you already know "the entire text"... So what do you actually mean by that? Very confused!

    – Dimitre Novatchev Oct 16 '20 at 00:38

3 Answers3

1

Try using

//p[contains(text(), 'Document Uploaded:&#160;Yes')]

&#160; is the Numeric character reference for the Named character reference &nbsp; (See Wikipedia). It can also be used in a DOCTYPE declaration at the beginning of an XML/XSLT document to make &nbsp; usuable:

<!DOCTYPE stylesheet [
<!ENTITY nbsp  "&#160;" >
]>
zx485
  • 28,498
  • 28
  • 50
  • 59
  • I guess the handling depends on the XPath-Parser. I tested it with `xsltproc` and `Saxon9`, and there it works. – zx485 Oct 06 '20 at 19:46
1

Use:

//p[. = 'Document Uploaded:&#xA0;Yes']

XSLT - based verification:

This XSLT transformation just evaluates the above XPath expression and outputs the result of the evaluation:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output omit-xml-declaration="yes" indent="yes"/>

  <xsl:template match="/">
    <xsl:copy-of select=
    "//p[. = 'Document Uploaded:&#xA0;Yes']"/>
  </xsl:template>
</xsl:stylesheet>

When applied on the provided XML document:

<!DOCTYPE stylesheet [
<!ENTITY nbsp  "&#160;" >
]>
<tr>
 <td align="left" width="200">
  <p>Document Uploaded:&nbsp;Yes</p>
 </td>
</tr>

the wanted, correct result is produced:

<p>Document Uploaded: Yes</p>
Dimitre Novatchev
  • 240,661
  • 26
  • 293
  • 431
0

Odd one this! The other answers mentioned above should word.

An alternative solution is to work around the character:

//p[contains(text(), 'Document Uploaded:')][contains(text(), 'Yes')]

Use an xpath that looks for an element that contains both string you need. If you need it to be more rigid, you can use starts-with and ends-with.

Another approach is don't find this item by text but rather get the element and process it in Java. I can't give a working example as I would need to see more html but the process would be:

  • Find an fixed anchor point e.g. something else in your html table you can identify easily
  • Create an xpath from that anchor with following-sibling:: or parent:: or following:: (you get the idea) - whatever to find this Document Uploaded: element
  • set that as your xpath in java with findElement.
  • In java: myElement.getText()

I think Selenium is smart enough to strip out the nbsp character - but even if it does not you'll still have a text string confirming your document upload status.

RichEdwards
  • 3,423
  • 2
  • 6
  • 22