How to check if an element is visible with WebDriver

后端 未结 11 1381
臣服心动
臣服心动 2020-12-04 13:14

With WebDriver from Selenium 2.0a2 I am having trouble checking if an element is visible.

WebDriver.findElement returns a WebElement<

相关标签:
11条回答
  • 2020-12-04 13:56

    I have the following 2 suggested ways:

    1. You can use isDisplayed() as below:

      driver.findElement(By.id("idOfElement")).isDisplayed();
      
    2. You can define a method as shown below and call it:

      public boolean isElementPresent(By by) {
        try {
          driver.findElement(by);
          return true;
        }
      catch (org.openqa.selenium.NoSuchElementException e) {
          return false;
        }
      }
      

    Now, you can do assertion as below to check either the element is present or not:

    assertTrue(isElementPresent(By.id("idOfElement")));
    
    0 讨论(0)
  • 2020-12-04 14:00

    Verifying ele is visible.

    public static boolean isElementVisible(final By by)
        throws InterruptedException {
            boolean value = false;
    
            if (driver.findElements(by).size() > 0) {
                value = true;
            }
            return value;
        }
    
    0 讨论(0)
  • 2020-12-04 14:00

    try this

    public boolean isPrebuiltTestButtonVisible() {
        try {
    
            if (preBuiltTestButton.isEnabled()) {
    
                return true;
    
            } else {
    
                return false;
            }
    
        } catch (Exception e) {
    
            e.printStackTrace();
            return false;
        }
    }
    
    0 讨论(0)
  • 2020-12-04 14:01
    public boolean isElementFound( String text) {
            try{
                WebElement webElement = appiumDriver.findElement(By.xpath(text));
                System.out.println("isElementFound : true :"+text + "true");
            }catch(NoSuchElementException e){
                System.out.println("isElementFound : false :"+text);
                return false;
            }
            return true;
        }
    
        text is the xpath which you would be passing when calling the function.
    the return value will be true if the element is present else false if element is not pressent
    
    0 讨论(0)
  • 2020-12-04 14:05

    Here is how I would do it (please ignore worry Logger class calls):

    public boolean isElementExist(By by) {
        int count = driver.findElements(by).size();
        if (count>=1) {
            Logger.LogMessage("isElementExist: " + by + " | Count: " + count, Priority.Medium);
            return true;
        }
        else {
            Logger.LogMessage("isElementExist: " + by + " | Could not find element", Priority.High);
            return false;
        }   
    }
    
    public boolean isElementNotExist(By by) {
        int count = driver.findElements(by).size();
        if (count==0) {
            Logger.LogMessage("ElementDoesNotExist: " + by, Priority.Medium);
            return true;
        }
        else {
            Logger.LogMessage("ElementDoesExist: " + by, Priority.High);
            return false;
        }   
    }
    
    public boolean isElementVisible(By by) {
        try {
            if (driver.findElement(by).isDisplayed()) {
                Logger.LogMessage("Element is Displayed: " + by, Priority.Medium);
                return true;
            }
        }
        catch(Exception e) {       
            Logger.LogMessage("Element is Not Displayed: " + by, Priority.High);
            return false;
        }       
    
        return false;
    }
    
    0 讨论(0)
提交回复
热议问题