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

问题描述 投票:2回答:3

有没有办法使用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注释来保持框架的一致性。有没有办法做到这一点?

java selenium pageobjects webdriverwait page-factory
3个回答
1
投票

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例外。


1
投票

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

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

Reference

希望它能帮到你!


1
投票

如果您希望元素不可见,则在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
  • 代码块: 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 } }

作为替代方案,您还可以使用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
  • 代码块: 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; } }

你可以在How to use explicit waits with PageFactory fields and the PageObject pattern找到详细的讨论

© www.soinside.com 2019 - 2024. All rights reserved.