使用WebDriverWait布尔值的NullPointerException

Mos*_*ser 2 java testng selenium selenium-webdriver

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

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

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

这是来自另一个调用wait的类的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");
}
Run Code Online (Sandbox Code Playgroud)

任何有关这方面的帮助将不胜感激!

nih*_*neo 6

 if (attribute.equals(null)) {
    return true;
  }`
Run Code Online (Sandbox Code Playgroud)

如果attribute为null,那么.equals调用将导致NPE.尝试使用attribute == null.

  • 这解决了我的问题!如此简单,又如此辉煌!感谢那! (2认同)