5

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 "".

kjhughes
  • 106,133
  • 27
  • 181
  • 240
xxx2017
  • 207
  • 1
  • 5
  • 15

2 Answers2

4

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.

Andrei U
  • 1,908
  • 1
  • 11
  • 14
1

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

kjhughes
  • 106,133
  • 27
  • 181
  • 240