Ђорђе Ђашић
Ђорђе Ђашић

Reputation: 121

Xpath code is working in Katalon but doesn't work in Selenium Java

I am using Katalon addon for Chrome which works fine. But I wanted to create small app with Selenium in Java to do simmilar thing. I am having the problem with xpath which is working with Katalon on chrome, but it is not working when I use it in my Java application in Selenium.

This it the xpath from Katalon:

xpath=(.//*[normalize-space(text()) and normalize-space(.)='Incorrect: Type your incorrect message here'])[1]/following::label[1]

And this is what I use with Selenium in Java:

WebElement el = driver_1.findElement(By.xpath("(.//*[normalize-space(text()) and normalize-space(.)=\'Incorrect: Type your incorrect message here\'])[1]/following::label[1]"));

It returns:

Exception in thread "AWT-EventQueue-0" org.openqa.selenium.NoSuchElementException: no such element: Unable to locate element:

Upvotes: 0

Views: 207

Answers (1)

Nandan A
Nandan A

Reputation: 2922

Special character \ is being added in your xPath I guess it's happened while you copy paste. Please use the below xPath,

driver.findElement(By.xpath("(.//*[normalize-space(text()) and normalize-space(.)='Incorrect: Type your incorrect message here'])[1]/following::label[1]"));

If you still unable to access element then please use some Explicit wait statement.

    WebDriverWait wait = new WebDriverWait(driver, 10);
    wait.until(ExpectedConditions.visibilityOfAllElementsLocatedBy(By.xpath(
            "(.//*[normalize-space(text()) and normalize-space(.)='Incorrect: Type your incorrect message here'])[1]/following::label[1]")));
    driver.findElement(By.xpath(
            "(.//*[normalize-space(text()) and normalize-space(.)='Incorrect: Type your incorrect message here'])[1]/following::label[1]"));

Upvotes: 1

Related Questions