Reputation: 367
I am getting the following error.
Exception in thread "main" org.openqa.selenium.NoSuchElementException: no such element
(Session info: chrome=39.0.2171.95)
From the looks of the error message it says that it couldnt find such element. So i added a wait until the element appears. The funny thing is that the error occurs on the line driver.findElement which means that the wait was able to find the element.
The question is obviously why is selenium not able to find the element.
At first i thought it was because of using a variable in the string
driver.findElement(By.id("_ctl0_ContentPlaceHolder1_eoiSectionSummary_individualRepeater__ctl0_sectionRepeater__ct" + i + "_isCompleteLabel")).getText();
So i tried to store the string somewhere and then findElement with it. As you see in the code below i have tried using print to verify that the string is the same as the one in the web. And they do match.
Im currently out of ideas now. Please help. Please let me know if you need any other information
public int verifyCompletion() {
wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("_ctl0_ContentPlaceHolder1_eoiSectionSummary_individualRepeater__ctl0_sectionRepeater__ctl0_isCompleteLabel")));
int uncompletedCounter = 0;
for (int i = 10; i < 20; i++) {
String text = "_ctl0_ContentPlaceHolder1_eoiSectionSummary_individualRepeater__ctl0_sectionRepeater__ct" + i + "_isCompleteLabel";
driver.findElement(By.id(text)).getText();
System.out.println(text);
boolean sectionCompleted =text.equalsIgnoreCase("Yes");
if (!sectionCompleted) {
uncompletedCounter++;
}
}
return uncompletedCounter;
}
Upvotes: 1
Views: 657
Reputation: 16201
I see a small bug in your selector. You are not parameterizing the selector correctly. I am not sure if it a very efficient way to handle this scenario though.
String selector = "_ctl" + i + "_ContentPlaceHolder1_eoiSectionSummary_individualRepeater__ctl" + i + "_sectionRepeater__ctl" + i + "_isCompleteLabel";
Edit: more precise code should look like this:
public int verifyCompletion() {
int uncompletedCounter = 0;
for (int i = 0; i < 10; i++) {
String selector = "_ctl" + i + "_ContentPlaceHolder1_eoiSectionSummary_individualRepeater__ctl" + i + "_sectionRepeater__ctl" + i + "_isCompleteLabel";
(new WebDriverWait(driver, 10)).until(ExpectedConditions.presenceOfElementLocated(By.id(selector)));
String elementText = driver.findElement(By.id(selector)).getText();
System.out.println(selector);
System.out.println(elementText);
boolean sectionCompleted =text.equalsIgnoreCase("Yes");
if (!sectionCompleted) {
uncompletedCounter++;
}
}
return uncompletedCounter;
}
Upvotes: 2