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.
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.
Thanks for reading. I hope you learned something interesting about React!
If you found this article useful, please share it with others. Don’t forget to subscribe to get notified for the upcoming articles.