我使用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的PresenceOfAllElementsLocatedBy实现:
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而不是。
如何重写PresenceOfAllElementsLocated方法以将WebElement作为参数?理由,PageFactory可以避免StaleElementException。
谢谢!
答案 0 :(得分:0)
您不需要重写内置方法。如果您只有一个WebElement
,则可以使用以下方法检查它是否存在(或者至少在页面上不再显示)
myWait.until(ExpectedConditions.invisibilityOf(element));
答案 1 :(得分:0)
请勿在代码
下使用pageFactory1.在您的页面对象类中定义web元素,如下所示
按元素= By.xpath(&#34; //选择[@id =&#39; ******&#39;]&#34;);
WebElement元素;
2.创建一个CommonMethod.java类并写下2方法
private static WebDriverWait jsWait = new WebDriverWait(driver,60);
public static WebElement getWebelement(WebDriver驱动程序,By by) {
WebElement ele = null;
try {
ele =Findelement(by);
} catch (StaleElementReferenceException ignored) {
Findelement(by);
}
return ele;
}
public static WebElement Findelement(By by)
{
jsWait.until(ExpectedConditions.visibilityOfElementLocated((by)));
WebElement ele = driver.findElement(by);
return ele;
}
3.致电此方法
CommonClass.getWebelement(驱动程序,元件);