没有这样的元素异常消息

时间:2014-08-01 21:56:08

标签: html selenium-webdriver

我正在使用eclipse juno并测试应用程序actitime,它在登录页面“keepLo​​ggedInCheckBox”中有一个复选框

它的HTML源代码,

<input type="checkbox" title="Do not select if this computer is shared" 
id="keepLoggedInCheckBox" value="on" name="remember">

我正在尝试使用

找到复选框“keepLo​​ggedInCheckBox”
WebElement check = driver.findElement(By.id("keepLoggedInCheckBox"));

但是得到了这个错误,

  

线程“main”中的异常org.openqa.selenium.NoSuchElementException:   无法找到元素:   { “方法”: “ID”, “选择器”: “keepLo​​ggedInCheckBox”}

我尝试使用xpath(// input [@ id ='keepLo​​ggedInCheckBox']),同样出错。

请帮帮我,解决这个问题。

2 个答案:

答案 0 :(得分:1)

我遇到了同样的问题。 DOM丢失了对相关元素的引用。它可以是 StaleStateReferenceException NoSuchElementException 。有两种方法可以处理这种情况。 (虽然我的解决方案是Java。基本概念是相同的。)

通过使用以下方法,您可以尝试单击元素。如果抛出异常,则捕获异常并尝试再次单击,直到元素存在:

public boolean retryingFindClick(By by) {
    boolean result = false;
    int attempts = 0;
    while(attempts < 2) {
        try {
            Actions action = new Actions(driver);
            WebElement userClick = wait.until(ExpectedConditions.presenceOfElementLocated(by));
            action.moveToElement(userClick).click().build().perform();
            driver.findElement(by).click();
            result = true;
            break;
        } catch(StaleElementReferenceException e) {
            System.out.println("StaleElementReferenceException");
        }
        catch(NoSuchElementException e) {
            System.out.println("No Such Element Found");
        }
        attempts++;
    }

    return result;
}

答案 1 :(得分:0)

请试试这个。我已经添加了implicitlyWait,它将允许你的DOM内容加载 注意:请使用确切的网址替换您的网址。

WebDriver driver=new FirefoxDriver();
    driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
    driver.get("Your URL");
    driver.findElement(By.xpath("//*[@id='keepLoggedInCheckBox']")).click();
相关问题