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

て烟熏妆下的殇ゞ 提交于 2019-12-12 19:25:32

问题


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

When using:

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

to locate the element, and then:

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

I would get:

org.opeqa.selenium.WebElement cannot be converted to org.openqa.selenium.By

I am able to do what I need by using:

wait.until(ExpectedConditions.invisibilityOfElementLocated(By.cssSelector('loading-content')));

However, I would like to be able to use the PageFactory annotations in order to keep the framework consistent. Is there a way to do this?


回答1:


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.




回答2:


You can use the correct expected condition, also:

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

Reference.

Hope it helps you!




回答3:


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



来源:https://stackoverflow.com/questions/54669417/how-to-wait-for-invisibility-of-an-element-through-pagefactory-using-selenium-an

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!