通过使用WebElement而不是By的方法对Allelement的java表示进行延迟
我正在使用PageFactory定位元素,我需要等待元素不存在
我的等待方法如下:
public void waitForElementIsNoExists(By by) {
try {
FluentWait<WebDriver> myWait = new FluentWait<WebDriver>(driver)
.withTimeout(timeOut, TimeUnit.SECONDS)
.pollingEvery(50, TimeUnit.MILLISECONDS)
.ignoring(NoSuchElementException.class)
.ignoring(StaleElementReferenceException.class)
.ignoring(TimeoutException.class);
myWait.until(ExpectedConditions.not(ExpectedConditions.presenceOfAllElementsLocatedBy(by)));
} catch (TimeoutException e) {
System.out.println("Timed out after default time out. Page is not responding after " + timeOut + "sec.");
}
}
通过Selenium API实现延迟的Allelements的存在:
public static ExpectedCondition<List<WebElement>> presenceOfAllElementsLocatedBy(final By locator) {
return new ExpectedCondition<List<WebElement>>() {
public List<WebElement> apply(WebDriver driver) {
List<WebElement> elements = ExpectedConditions.findElements(locator, driver);
return elements.size() > 0 ? elements : null;
}
public String toString() {
return "presence of any elements located by " + locator;
}
};
}
但是,由于PageFactory,我需要使用WebElement而不是by
如何重写PresenceOfAllElementsLocated方法以获取WebElement作为参数?因此,PageFactory可以避免StaleElementException
谢谢
# 1 楼答案
您不需要重写内置方法。如果您有一个
WebElement
,可以使用以下方法检查它是否不存在(或者至少不存在,它在页面上不再可见):# 2 楼答案
不要使用pageFactory使用下面的代码
一,。在页面对象类中定义web元素,如下所示
按元素=按。xpath(//select[@id='******'])
网络元素
二,。创建一个公共方法。java类和写在下面的2方法
私有静态WebDriverWait jsWait=新WebDriverWait(驱动程序,60)
公共静态WebElement getWebelement(WebDriver驱动程序,按) {
WebElement ele=null
公共静态WebElement Findelement(按)
三,。调用此方法
普通班。getWebelement(驱动程序,元素)