首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使用Selenium和Java通过PageFactory等待元素的不可见性

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

Stack Overflow用户
提问于 2019-02-13 11:45:37
回答 4查看 9.7K关注 0票数 5

是否有一种方法可以使用PageFactory注释来等待Selenium中不存在的元素?

使用时:

代码语言:javascript
复制
@FindBy(css= '#loading-content')
WebElement pleaseWait;

若要定位元素,然后:

代码语言:javascript
复制
wait.until(ExpectedConditions.invisibilityOfElementLocated(pleaseWait));

我会得到:

代码语言:javascript
复制
org.opeqa.selenium.WebElement cannot be converted to org.openqa.selenium.By

我可以通过以下方式来做我需要的事情:

代码语言:javascript
复制
wait.until(ExpectedConditions.invisibilityOfElementLocated(By.cssSelector('loading-content')));

但是,为了保持框架的一致性,我希望能够使用PageFactory注释。有办法这样做吗?

EN

回答 4

Stack Overflow用户

回答已采纳

发布于 2019-02-13 12:11:36

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

代码语言:javascript
复制
wait.until(ExpectedConditions.invisibilityOfAllElements(Arrays.asList(pleaseWait)));

最新答案:

如果要检查元素是否存在于页面中,则可以检查其列表大小是否等于0,因为当其不在UI上显示时,其列表大小将为0。

您可以通过以下方法获得元素的列表:

代码语言:javascript
复制
@FindBy(css='#loading-content')
List<WebElement> pleaseWait;

并且可以使用以下方法检查列表大小等于0:

代码语言:javascript
复制
if(pleaseWait.size()==0){
     System.out.println("Element is not visible on the page");
     // Add the further code here
}

这也不会给NoSuchElement带来例外。

票数 2
EN

Stack Overflow用户

发布于 2019-02-13 13:09:03

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

invisibilityOfElementLocated()

invisibilityOfElementLocated()是用于检查元素是否不可见或不存在于DOM中的期望的实现。它的定义如下:

代码语言:javascript
复制
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
  • 代码块: 导入org.openqa.selenium.WebDriver;导入org.openqa.selenium.WebElement;导入org.openqa.selenium.support.FindBy;导入org.openqa.selenium.support.PageFactory;导入org.openqa.selenium.support.ui.WebDriverWait;公共类fooPage { WebDriver驱动程序;公共fooPage(WebDriver驱动程序,此);}//您不需要此//@FindBy(css=‘# need content’) //WebElement pleaseWait;公开void (){ bool =新的WebDriverWait(驱动程序,WebDriverWait//其他代码行)}

作为另一种选择,您还可以使用invisibilityOf()方法如下:

invisibilityOf()

invisibilityOf()是用于检查元素不可见性的期望的实现。它的定义如下:

代码语言:javascript
复制
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
  • 代码块: 导入org.openqa.selenium.WebDriver;导入org.openqa.selenium.WebElement;导入org.openqa.selenium.support.FindBy;导入org.openqa.selenium.support.PageFactory;导入org.openqa.selenium.support.ui.WebDriverWait;公共类fooPage { WebDriver驱动程序;公共fooPage(WebDriver驱动程序){PageFactory.initElements(驱动程序,此);} @FindBy(css=‘#loading content’) WebElement pleaseWait;{ bool =新WebDriverWait(驱动程序,WebDriverWait/其他代码行)公共WebElement getWebElement() {返回pleaseWait;}

您可以在如何在PageFactory字段和PageObject模式中使用显式等待中找到详细的讨论

票数 3
EN

Stack Overflow用户

发布于 2019-02-13 12:16:36

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

代码语言:javascript
复制
wait.until(ExpectedConditions.invisibilityOf(pleaseWait));

参考文献

希望它能帮到你!

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/54669417

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档