【问题标题】:NullPointerException using WebDriverWait BooleanNullPointerException 使用 WebDriverWait 布尔值
【发布时间】:2016-07-08 09:47:05
【问题描述】:

我有两个类,一个运行单击按钮等的方法。在页面上,有一个按钮被禁用,我有一个 WebDriverWait 通过检查属性“禁用”来等待它再次启用" 已从 html 元素中删除。但是,当我运行测试时,我得到一个 nullPointerException。我想我知道它的来源,但在试图解决它时遇到了问题。

这是运行以执行操作的方法:

public void methodThatRuns(WebDriver driver) throws InterruptedException {
    properties.inputTxt(driver, "100");
    sundries.waitEnabledButton(driver, properties.nextButton(driver));
    properties.nextButton(driver).click();
}   

这是它调用等待的另一个类的 waitEnabledButton 方法:

public void waitEnabledButton(WebDriver driver, final WebElement btn) throws NullPointerException {
    WebDriverWait wait = new WebDriverWait(driver, 10);
    System.out.println("Starting the wait");
    try {
        wait.until(new ExpectedCondition<Boolean>(){
            public Boolean apply(WebDriver driver) {
                final String attribute = btn.getAttribute("disabled");
                if (attribute.equals(null)) {
                    return true;
                }
                else {
                    return false;
                }
            }
        });
    } catch (StaleElementReferenceException e) {
        System.out.println("The disabled attribute was destroyed successfully and the script can continue."); //using this as the attribute gets destroyed when the button is enabled which throws a staleElement exception
    } 
    System.out.println("Wait is over");
}

对此的任何帮助将不胜感激!

【问题讨论】:

    标签: java selenium selenium-webdriver testng


    【解决方案1】:
     if (attribute.equals(null)) {
        return true;
      }`
    

    如果属性为空,则 .equals 调用将导致 NPE。尝试使用属性 == null。

    【讨论】:

    • 解决了我的问题!如此简单,却又如此精彩!谢谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-05-23
    • 1970-01-01
    • 2017-04-08
    • 2020-09-01
    • 2020-06-22
    • 1970-01-01
    相关资源
    最近更新 更多