首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >硒NoSuchElementException

硒NoSuchElementException
EN

Stack Overflow用户
提问于 2015-01-12 22:18:56
回答 1查看 637关注 0票数 1

我得到了以下错误。

代码语言:javascript
复制
Exception in thread "main" org.openqa.selenium.NoSuchElementException: no such element
 (Session info: chrome=39.0.2171.95)

从错误消息的外观来看,它说找不到这样的元素。因此,我添加了一个等待,直到元素出现。有趣的是,错误发生在行driver.findElement上,这意味着等待能够找到元素。

问题很明显,为什么selenium无法找到元素。

一开始我以为是因为在字符串中使用了一个变量。

代码语言:javascript
复制
driver.findElement(By.id("_ctl0_ContentPlaceHolder1_eoiSectionSummary_individualRepeater__ctl0_sectionRepeater__ct" + i + "_isCompleteLabel")).getText();

所以我尝试将字符串存储在某个地方,然后用它存储findElement。正如您在下面的代码中所看到的,我尝试使用print来验证字符串是否与web中的字符串相同。而且它们是匹配的。

我现在没有主意了。请帮帮忙。如果你需要其他信息,请告诉我。

代码语言:javascript
复制
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;


}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-01-12 22:42:11

我看到你的选择器里有个小虫子。您没有正确地参数化选择器。不过,我不确定这是否是一种非常有效的处理这种情况的方法。

代码语言:javascript
复制
String selector  = "_ctl" + i + "_ContentPlaceHolder1_eoiSectionSummary_individualRepeater__ctl" + i + "_sectionRepeater__ctl" + i + "_isCompleteLabel";

编辑:更精确的代码应该如下所示:

代码语言:javascript
复制
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;
}
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/27911734

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档