是否有一种方法可以使用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')));但是,为了保持框架的一致性,我希望能够使用PageFactory注释。有办法这样做吗?
发布于 2019-02-13 12:11:36
invisibilityOfElementLocated需要一个定位器,但是您正在发送一个web元素,这就是它抛出错误的原因。您可以通过使用以下方法检查webelement列表来执行该操作:
wait.until(ExpectedConditions.invisibilityOfAllElements(Arrays.asList(pleaseWait)));最新答案:
如果要检查元素是否存在于页面中,则可以检查其列表大小是否等于0,因为当其不在UI上显示时,其列表大小将为0。
您可以通过以下方法获得元素的列表:
@FindBy(css='#loading-content')
List<WebElement> pleaseWait;并且可以使用以下方法检查列表大小等于0:
if(pleaseWait.size()==0){
System.out.println("Element is not visible on the page");
// Add the further code here
}这也不会给NoSuchElement带来例外。
发布于 2019-02-13 13:09:03
在PageObjectModel中使用PageFactory时,如果您希望元素是不可见的,则可以使用普通定位器工厂的显式等待支持,并使用以下任何一种解决方案:
invisibilityOfElementLocated()
invisibilityOfElementLocated()是用于检查元素是否不可见或不存在于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作为另一种选择,您还可以使用invisibilityOf()方法如下:
invisibilityOf()
invisibilityOf()是用于检查元素不可见性的期望的实现。它的定义如下:
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您可以在如何在PageFactory字段和PageObject模式中使用显式等待中找到详细的讨论
发布于 2019-02-13 12:16:36
https://stackoverflow.com/questions/54669417
复制相似问题