How to wait for invisibility of an element through PageFactory using Selenium and Java

后端 未结 3 838
攒了一身酷
攒了一身酷 2020-12-21 04:28

Is there a way to wait for an element not present in Selenium using PageFactory annotations?

When using:

@FindBy(css= \'#loading-content\')
WebEleme         


        
相关标签:
3条回答
  • 2020-12-21 04:31

    When using PageFactory in PageObjectModel if you expect the element to be invisible, you can use the Explicit Wait support with a normal locator factory and use either of the following solutions:


    invisibilityOfElementLocated()

    invisibilityOfElementLocated() is the implementation for an expectation for checking that an element is either invisible or not present on the DOM. It is defined as follows:

    public static ExpectedCondition<java.lang.Boolean> invisibilityOfElementLocated(By locator)
    An expectation for checking that an element is either invisible or not present on the DOM.
    
    Parameters:
        locator - used to find the element
    
    Returns:
        true if the element is not displayed or the element doesn't exist or stale element
    
    • Code Block:

      import org.openqa.selenium.WebDriver;
      import org.openqa.selenium.WebElement;
      import org.openqa.selenium.support.FindBy;
      import org.openqa.selenium.support.PageFactory;
      import org.openqa.selenium.support.ui.WebDriverWait;
      
      public class fooPage {
      
          WebDriver driver;
          public fooPage(WebDriver driver)
          {
              PageFactory.initElements(driver, this);
          }
      
          //you don't need this
          //@FindBy(css= '#loading-content')
          //WebElement pleaseWait;
      
          public void foo()
          {
              Boolean bool = new WebDriverWait(driver, 20).until(ExpectedConditions.invisibilityOfElementLocated(By.cssSelector('#loading-content')));
              //other lines of code
          }
      }
      

    As an alternative you can also use the invisibilityOf() method as follows:

    invisibilityOf()

    invisibilityOf() is the implementation for an expectation for checking the element to be invisible. It is defined as follows:

    public static ExpectedCondition<java.lang.Boolean> invisibilityOf(WebElement element)
    An expectation for checking the element to be invisible
    
    Parameters:
        element - used to check its invisibility
    
    Returns:
        Boolean true when elements is not visible anymore
    
    • Code Block:

      import org.openqa.selenium.WebDriver;
      import org.openqa.selenium.WebElement;
      import org.openqa.selenium.support.FindBy;
      import org.openqa.selenium.support.PageFactory;
      import org.openqa.selenium.support.ui.WebDriverWait;
      
      public class fooPage {
      
          WebDriver driver;
          public fooPage(WebDriver driver)
          {
              PageFactory.initElements(driver, this);
          }
      
      
          @FindBy(css= '#loading-content')
          WebElement pleaseWait;
      
          public void foo()
          {
              Boolean bool = new WebDriverWait(driver, 20).until(ExpectedConditions.invisibilityOf(fooPage.getWebElement()));
              //other lines of code
          }
      
          public WebElement getWebElement()
          {
              return pleaseWait;
          }
      }
      

    You can find a detailed discussion in How to use explicit waits with PageFactory fields and the PageObject pattern

    0 讨论(0)
  • 2020-12-21 04:46

    You can use the correct expected condition, also:

    wait.until(ExpectedConditions.invisibilityOf(pleaseWait));
    

    Reference.

    Hope it helps you!

    0 讨论(0)
  • 2020-12-21 04:48

    invisibilityOfElementLocated is expecting a locator but you are sending a web-element and that is why it is throwing an error. You can perform the operation by checking the webelement list by using:

    wait.until(ExpectedConditions.invisibilityOfAllElements(Arrays.asList(pleaseWait)));
    

    Updated Answer:
    If you want to check that the element is not present on the page then you can check its list size is equal to 0 or not, as its list size will be 0 when its not displayed on the UI.

    You can get the list of the element by using:

    @FindBy(css='#loading-content')
    List<WebElement> pleaseWait;
    

    And you can check the list size equals to 0 by using:

    if(pleaseWait.size()==0){
         System.out.println("Element is not visible on the page");
         // Add the further code here
    }
    

    And this would not give NoSuchElement exception as well.

    0 讨论(0)
提交回复
热议问题