Arun Chettur
Arun Chettur

Reputation: 43

How to write xpath in selenium for a text that contains Apostrophe (')

I have the following text and i need to write x path

Please upload this owner's ID:

Here when i write x path with text or contains text it doesn't return the matches.

//*[@text()='Please upload this owner's ID:']

I doubt that there is an ' symbol in the word owner's and hence its not giving proper result.

Hope i can find a solution here

Upvotes: 0

Views: 4029

Answers (2)

StuartLC
StuartLC

Reputation: 107237

Although the escape sequence for a single quote (') in Xml is ', there's a limitation in XPath 1.0 preventing ' from being used directly in a path.

Also, as it stands, your question asks for any element with a text attribute containing the search string, e.g.

 <node text="Please upload this owner's ID:" />

Assuming there are no other text attributes with strings containing those two phases, a hacky approximation to the search would be:

//*[contains(@text, 'Please upload this owner') and contains(@text, 's ID:')]

If however you meant that the element's text() content needed to match (i.e. not a text attribute):

 <node>Please upload this owner's ID</node>

Then the same hack would be:

//*[contains(text(), 'Please upload this owner') and contains(text(), 's ID:')]

Upvotes: 1

Murthi
Murthi

Reputation: 5347

try with double quotes.

 //*[@text()="Please upload this owner's ID:"]

or with escape

 //*[@text()='Please upload this owner\'s ID:']

Upvotes: 2

Related Questions