How to use apostrophe (') in xpath while finding element using webdriver?

Use the xpath as shown below:

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

Hope this helps.


You have to use double-quotes as your XPath string literal delimiter, since XPath 1.0 doesn't provide a way of escaping quotes. In addition to that, you can escape the double-quotes in Java, to avoid it from conflicting with your Java string delimiter, which also use double-quotes :

driver.findelements(By.xpath("//input[@text=\"WE'd like to hear from you\"]"))