1

When the webpage has text test'"message { both single & double quotes in it}. I have to find it using Xpath

As many said, in forums I looked in for the right approaches, tried using '' as escape character. But it didn't work, as expected!

Hence my attempts & errors are below & How do I achieve the expected result ?

the escape character didn't help in locating the element within browser DOM Still I tried in script, to see any miracle happens, But just the errors.

  1. Attempt with 'single escape character'

d.FindElement(By.XPath("//div[text()=\"test\"message\"]"));

Error: Unable to locate an element with the xpath expression //div[text()="test"message"] because of the following error: SyntaxError: Failed to execute 'evaluate' on 'Document': The string '//div[text()="test"message"]' is not a valid XPath expression.

  1. Attempt with 'double escape character' as the scripting language is c#

d.FindElement(By.XPath("//div[text()=\"test\\\"message\"]"));

Error: Unable to locate an element with the xpath expression //div[text()="test\"message"] because of the following error: SyntaxError: Failed to execute 'evaluate' on 'Document': The string //div[text()="test\"message"] is not a valid XPath expression.

Aravinth
  • 254
  • 3
  • 14
  • Update the question with the relevant HTML. – undetected Selenium Sep 06 '20 at 22:00
  • @DebanjanB why???? How that will improve this question? Will it make any more unique that dozens of other "XPath mixed quotes" questions? – Alexei Levenkov Sep 06 '20 at 22:01
  • @AlexeiLevenkov Considering Selenium's usage of xpath v1.0 I'm sure taking into account the ancestor/parent/sibling elements there can be better xpaths/cssSelectors. – undetected Selenium Sep 06 '20 at 22:11
  • Due to the fact that this was marked as duplicate, i am adding the solution that worked for me here as common. In case someone landed in this question, 1. Split the element text based on quotes type - like, actual text to be splitted as < test' message" on' web> or similar 2. use combined contains with alternative quotes as outside quotes – Aravinth Sep 19 '20 at 11:47

0 Answers0