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

后端 未结 3 937
攒了一身酷
攒了一身酷 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 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 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

提交回复
热议问题