Search code examples
c#htmlxmlxpathwebdriver

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


Solution

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