如何使用Selenium和Java通过PageFactory等待元素不可见

如何使用Selenium和Java通过PageFactory等待元素不可见,java,selenium,pageobjects,webdriverwait,page-factory,Java,Selenium,Pageobjects,Webdriverwait,Page Factory,是否有方法使用PageFactory注释等待Selenium中不存在的元素 使用时: @FindBy(css= '#loading-content') WebElement pleaseWait; 要定位元素,请执行以下操作: wait.until(ExpectedConditions.invisibilityOfElementLocated(pleaseWait)); 我会得到: org.opeqa.selenium.WebElement cannot be converted to or

是否有方法使用PageFactory注释等待Selenium中不存在的元素

使用时:

@FindBy(css= '#loading-content')
WebElement pleaseWait;
要定位元素,请执行以下操作:

wait.until(ExpectedConditions.invisibilityOfElementLocated(pleaseWait));
我会得到:

org.opeqa.selenium.WebElement cannot be converted to org.openqa.selenium.By
我可以通过以下方式完成我需要的工作:

wait.until(ExpectedConditions.invisibilityOfElementLocated(By.cssSelector('loading-content')));
wait.until(ExpectedConditions.invisibilityOfAllElements(Arrays.asList(pleaseWait)));
@FindBy(css='#loading-content')
List<WebElement> pleaseWait;
if(pleaseWait.size()==0){
     System.out.println("Element is not visible on the page");
     // Add the further code here
}

但是,我希望能够使用PageFactory注释来保持框架的一致性。有什么方法可以做到这一点吗?

InvisibilityFelementLocated
需要一个定位器,但您正在发送一个web元素,这就是它抛出错误的原因。您可以通过以下方式检查webelement列表来执行该操作:

wait.until(ExpectedConditions.invisibilityOfElementLocated(By.cssSelector('loading-content')));
wait.until(ExpectedConditions.invisibilityOfAllElements(Arrays.asList(pleaseWait)));
@FindBy(css='#loading-content')
List<WebElement> pleaseWait;
if(pleaseWait.size()==0){
     System.out.println("Element is not visible on the page");
     // Add the further code here
}
更新答案:
如果要检查元素在页面上是否不存在,则可以检查其列表大小是否等于0,因为当其未显示在UI上时,其列表大小将为0

您可以使用以下命令获取元素列表:

wait.until(ExpectedConditions.invisibilityOfElementLocated(By.cssSelector('loading-content')));
wait.until(ExpectedConditions.invisibilityOfAllElements(Arrays.asList(pleaseWait)));
@FindBy(css='#loading-content')
List<WebElement> pleaseWait;
if(pleaseWait.size()==0){
     System.out.println("Element is not visible on the page");
     // Add the further code here
}

这也不会产生NoTouchElement异常。

您可以使用正确的预期条件,也可以:

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


希望它能帮助你

PageObjectModel中使用PageFactory时,如果希望元素不可见,则可以对普通定位器工厂使用显式等待支持,并使用以下任一解决方案:


不可见性FelementLocated() 是检查元素是否在DOM上不可见或不存在的期望的实现。其定义如下:

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
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

您也可以使用
invisibilityOf()
方法,如下所示:

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
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
不可见性() 是检查元素不可见的期望的实现。其定义如下:

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
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

您可以在

中找到详细的讨论,我正在等待元素不可见。@mateše先生,是的,我明白了。我已经更新了我的答案。请再检查一遍。谢谢。我收到
预期条件失败:等待DefaultElementLocator“By.css”的代理元素不可见选择器:#加载内容(以500毫秒间隔尝试5秒)
错误消息。似乎元素消失得太快了。不,不起作用。我获得
预期条件失败:等待所有元素不可见[代理元素:DefaultElementLocator'By.css选择器:#加载内容](以500毫秒的间隔尝试5秒)
。然而,似乎下一步正在执行。是的,因此,我猜它之所以出现错误,是因为它希望元素不可见,但它是可见的。因为该方法起作用了,下一行代码也被执行了。这是上面消息的延续:
,原因是:org.openqa.selenium.NoSuchElementException:没有这样的元素:无法找到元素:{“方法”:“css选择器”,“选择器”:“#加载内容”}
。我认为它没有按预期工作。为什么它会抛出一个异常?如果你得到
NoTouchElementException
,这意味着该元素在页面上不可见,或者你的css不正确,通过它你可以找到该元素。请告诉我你想在这里验证的场景是什么,然后我就可以了为了更好地帮助你。