首页 > 解决方案 > 按钮不可见并返回 TRUE,按钮 xpath 在网页中,用于 selenium webdriver

问题描述

使用 selenium webdriver 测试按钮是否不可见,布尔值始终返回 true。

使用下面的代码并返回 TRUE,在网页中我可以看到 Add 按钮的 xpath。按钮不可见。

    boolean present;  
    try { 
        //driver.findElement(By.xpath("//button[@id='add']")); 
        driver.findElement(By.xpath("//button[@id='add']")).isDisplayed();
       present = true;
       System.out.println("Add button is present");   
    } catch (NoSuchElementException e) { 
       present = false;
       System.out.println("Add button is not present:" +e.getMessage()); 
}

xpath: addButtonxpath

标签: javaseleniumwebdriver

解决方案


这是代码片段

boolean present;
try {
    present = driver.findElement(By.xpath("//button[@id='add']")).isDisplayed();
    System.out.println("Add button is present");
} catch (NoSuchElementException e) {
    present = false;
    System.out.println("Add button is not present:" + e.getMessage());
} catch (Exception e) {
    present = false;
    System.out.println("Add button is not present:" + e.getMessage());
}

推荐阅读