Enroll Now for the new Online Course: How to Find XPath for Web Elements in Chrome and Firefox Browsers.

 

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).

For example, if the text you want to use to select an element is: We’ll , here is how you can find all web elements that match you apostrophe (single quote) text with Selenium WebDriver.

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

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.