How to use apostrophe (') in xpath when searching for an element using webdriver?

I need to use apostrophe (') in my xpath expression, which I need to use when searching for elements using webdriver

I need to use below Xpath expression

//input[@text="WE'd like to hear from you"] 

when using the above expression in the search element function, I replace double quotes with single quotes

 driver.findelements(By.xpath("//input[@text='WE'd like to hear from you']")) 
+6
source share
4 answers

Use xpath as shown below:

 driver.findElements(By.xpath("//input[contains(@text,\"WE'd\"]")); 

Hope this helps.

+8
source

You need to use double quotes as a literal delimiter for XPath strings, since XPath 1.0 does not provide a way to escape quotes. In addition to this, you can avoid double quotes in Java to avoid conflict with your Java string separator, which also use double quotes:

 driver.findelements(By.xpath("//input[@text=\"WE'd like to hear from you\"]")) 
+2
source

Using the Escape character is not for the purpose. I tried the concatenation function and worked like a charm. See below xpath.

tag: li Workflow / li Initiator Manager Manager

Combine the function and split the line as -

concat ("Workflow initiator manager", "'", "Manager")

A single quote is stored in double quotation marks , while other characters are stored in single quotation marks.

So XPath looks like

// li [. = concat ("Workflow initiator manager", "'", "Manager")]

Hope this helps. :)

0
source

if the above solution does not work, use the solution below using an escape sequence.

xpath: // li [. = \ "Workflow Initiator Manager Manager \"]

Here we treat all the text as a string using an escape character

0
source

All Articles