验证按钮未显示

时间:2018-10-30 14:48:13

标签: java selenium-webdriver

我尝试了以下解决方案以验证对于特定用户组没有显示该按钮。这些解决方案均无效。我在代码中没有这样的元素异常。 请让我知道我还有什么可以尝试的。

  try {
                    boolean btnPresence = driver.findElement(By.linkText("/html/body/div/div/div/main/div[2]/div[2]/div/form/button")).isDisplayed();

                }
           catch (org.openqa.selenium.NoSuchElementException e)
                {
                            return;
                }
                }
                Assert.assertTrue(driver.findElement(By.xpath("/html/body/div/div/div/main/div[2]/div[2]/div/form/button")).isDisplayed());
                if (driver.findElement(By.xpath("/html/body/div/div/div/main/div[2]/div[2]/div/form/button")).isDisplayed()) {
                    System.out.println("Fail! Submit button is displayed for a CMS Admin on the ORC TA Form.");}
                else {
                        System.out.println("Pass!!- Submit Button is not displayed for CMS Admin on the ORC TA Form");
                     }   

                boolean elePresent = driver.findElement(By.xpath("/html/body/div/div/div/main/div[2]/div[2]/div/form/button")).isDisplayed();
                boolean elePresent = driver.findElement(By.xpath("/html/body/div/div/div/main/div[2]/div[2]/div/form/button")).isDisplayed();
                boolean exist = driver.findElement(By.xpath("/html/body/div/div/div/main/div[2]/div[2]/div/form/button")).size() == 0;

1 个答案:

答案 0 :(得分:0)

您可以检查元素是否存在:

public boolean existsElement_byXpath(String xpath) {
        try {
            driver.findElement(By.xpath(xpath));
        } catch (NoSuchElementException e) {
            return false;
        }
        return true;
    }