Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I escape double quotes in a Xpath in Selenium?

For Example:

By.xpath("//*[@id="ext-gen1035"]/div/div[3]/i")
like image 377
Prabhu Avatar asked Apr 07 '16 14:04

Prabhu


People also ask

How do you escape characters in XPath?

There is no way to escape characters at the XPath level, so you can't have a literal XPath string containing both kinds of quote.

Can we use double quotes in XPath?

If I recall correctly, you can use either single or double quotes to surround a string in XPath.

How XPath handle single quotes?

XPath's string literals can't contain both types of quotes; you need to construct the string using the XPath concat function. For example, if you wanted to match the string "That's mine", he said. , you would need to do something like: text()=concat('"That', "'", 's mine", he said.

How do you pass an apostrophe in XPath?

The only reliable way of using XPath in Selenium WebDriver for text with apostrophes (single quotes) is to use double quotes for the expression of the XPath.


1 Answers

You can actually use single quotes too:

By.xpath("//*[@id='ext-gen1035']/div/div[3]/i")

Or escape double quotes with a backslash:

By.xpath("//*[@id=\"ext-gen1035\"]/div/div[3]/i")
like image 74
alecxe Avatar answered Oct 06 '22 11:10

alecxe