我得到了以下错误。
Exception in thread "main" org.openqa.selenium.NoSuchElementException: no such element
(Session info: chrome=39.0.2171.95)从错误消息的外观来看,它说找不到这样的元素。因此,我添加了一个等待,直到元素出现。有趣的是,错误发生在行driver.findElement上,这意味着等待能够找到元素。
问题很明显,为什么selenium无法找到元素。
一开始我以为是因为在字符串中使用了一个变量。
driver.findElement(By.id("_ctl0_ContentPlaceHolder1_eoiSectionSummary_individualRepeater__ctl0_sectionRepeater__ct" + i + "_isCompleteLabel")).getText();所以我尝试将字符串存储在某个地方,然后用它存储findElement。正如您在下面的代码中所看到的,我尝试使用print来验证字符串是否与web中的字符串相同。而且它们是匹配的。
我现在没有主意了。请帮帮忙。如果你需要其他信息,请告诉我。
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;
}发布于 2015-01-12 22:42:11
我看到你的选择器里有个小虫子。您没有正确地参数化选择器。不过,我不确定这是否是一种非常有效的处理这种情况的方法。
String selector = "_ctl" + i + "_ContentPlaceHolder1_eoiSectionSummary_individualRepeater__ctl" + i + "_sectionRepeater__ctl" + i + "_isCompleteLabel";编辑:更精确的代码应该如下所示:
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;
}https://stackoverflow.com/questions/27911734
复制相似问题