Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use XPath to select text with a quote character?

How can I select a xpath that contains a text with ""

Let say my text on the page is: "my text" (this includes the "").

When I make the xpath I do in VS:

"//td[contains(.,'"mytext">')]"

But VS doesnt see this as correct because it shows mytext in white as if it doesnt belong to the xpath

It says it is a syntax error and it expects an ','.

So how can I make an xpth that uses an contain where the text has an "".

like image 318
xxx2017 Avatar asked Oct 17 '22 19:10

xxx2017


2 Answers

The answer is pretty simple, escape the double quote characters:

"//td[contains(.,'\"mytext\">')]"

This is a reference of what characters need escaping: https://blogs.msdn.microsoft.com/csharpfaq/2004/03/12/what-character-escape-sequences-are-available/

Hope it helps.

like image 112
Andrei U Avatar answered Oct 20 '22 17:10

Andrei U


If the context of your XPath is XML, then escape the double quotes with ":

//td[contains(.,'"mytext">')]

See also: Simplified XML Escaping

If the context of your XPat is not XML, you might try

//td[contains(.,concat('"','mytext', '"', '>'))]

to see if VS is happier with that expression, or set a separate variable for " constant, and build up your XPath piecewise from it.

If that doesn't help in your context, see how to escape single quote in xslt substring function

like image 20
kjhughes Avatar answered Oct 20 '22 16:10

kjhughes