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. In order to find how to use XPath to locate WebElements in Chrome browser you can check our previous article.
For example, for the code below:
driver.findElement(By.xpath("//*[@text='" + text + "']"))
you need to change to:
driver.findElement(By.xpath("//*[@text=\"" + text + "\"]"))
This way, it will not matter if your text contains apostrophes or not, and you’ll be able to select web elements by text that contains apostrophes (single quotes).
LoadFocus.com is a cloud testing platform, use Automated Website Testing Service to build functional tests and automate regression manual tests of your website, more details.