We usually construct relative xpath using //tagName[@attribute='value']
Note value is surrounded with single quote.
Suppose there is a special character like "'" (apostrophe) in string. For eg:
"What We're Reading"
Then, when xpath is constructed like this -
WebElement findElement = driver.findElement(By.xpath("//h1[text()='What We're Reading']"));
This exception would be thrown in Selenium:
Exception in thread "main" org.openqa.selenium.InvalidSelectorException: invalid selector: Unable to locate an element with the xpath expression //h1[text()='What We're Reading'] because of the following error:
SyntaxError: Failed to execute 'evaluate' on 'Document': The string '//h1[text()='What We're Reading']' is not a valid XPath expression.
(Session info: chrome=68.0.3440.106)
To fix this error, you will have to redefine xpath something like this:
WebElement findElement = driver.findElement(By.xpath("//h1[text()=\"What We're Reading\"]"));
That is surrounding text with double quote in xpath and providing backslash character before ".
Note value is surrounded with single quote.
Suppose there is a special character like "'" (apostrophe) in string. For eg:
"What We're Reading"
Then, when xpath is constructed like this -
WebElement findElement = driver.findElement(By.xpath("//h1[text()='What We're Reading']"));
This exception would be thrown in Selenium:
Exception in thread "main" org.openqa.selenium.InvalidSelectorException: invalid selector: Unable to locate an element with the xpath expression //h1[text()='What We're Reading'] because of the following error:
SyntaxError: Failed to execute 'evaluate' on 'Document': The string '//h1[text()='What We're Reading']' is not a valid XPath expression.
(Session info: chrome=68.0.3440.106)
To fix this error, you will have to redefine xpath something like this:
WebElement findElement = driver.findElement(By.xpath("//h1[text()=\"What We're Reading\"]"));
That is surrounding text with double quote in xpath and providing backslash character before ".
No comments:
Post a Comment