Vivek Vivek - 1 month ago 8x
Java Question

Escape Apostrophe in Selenium

I am trying to escape an apostrophe in Selenium. Sample text below

<a title="My Day's Schedule"></a>

I am trying to search for this element using
//a[contains(@title , 'My Day's Schedule')]
, but this would not work since the apostrophe is not escaped.
Tried various methods such as using the double quote to escape the apostrophe / using the java escape character ; but the XPath does not recognize the element

Any pointers ?



Use a back-slash like \ before the sign you want to escape.

If you sometimes don't want so escape to much you can also use part of the attributes like:
//a[contains(@title, 's Schedule')]

Or with escape:
//a[@title='My Day\'s Schedule']

As an alternative you can also use other attributes like href instead of title.

Tip: use contains only when give part of the string, else use @attribute='string'