首页 > 解决方案 > 不显示验证按钮

问题描述

我尝试了以下解决方案来验证该按钮是否未针对特定用户组显示。这些解决方案均无效。我的代码没有这样的元素异常。如果还有什么我可以尝试的,请告诉我。

  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;

标签: javaselenium-webdriver

解决方案


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

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

推荐阅读