上面提到的WebElementWait类:
package org.openqa.selenium.support.ui;import java.util.concurrent.TimeUnit;import org.openqa.selenium.NotFoundException;import org.openqa.selenium.WebElement;public class WebElementWait extends FluentWait<WebElement> { public final static long DEFAULT_SLEEP_TIMEOUT = 500; public WebElementWait(WebElement element, long timeOutInSeconds) { this(element, new SystemClock(), Sleeper.SYSTEM_SLEEPER, timeOutInSeconds, DEFAULT_SLEEP_TIMEOUT); } public WebElementWait(WebElement element, long timeOutInSeconds, long sleepInMillis) { this(element, new SystemClock(), Sleeper.SYSTEM_SLEEPER, timeOutInSeconds, sleepInMillis); } protected WebElementWait(WebElement element, Clock clock, Sleeper sleeper, long timeOutInSeconds, long sleepTimeOut) { super(element, clock, sleeper); withTimeout(timeOutInSeconds, TimeUnit.SECONDS); pollingEvery(sleepTimeOut, TimeUnit.MILLISECONDS); ignoring(NotFoundException.class); }}它与WebDriverWait相同,只不过
WebDriver参数被替换为
WebElement。
然后,isValid方法:
//import com.google.common.base.Function; //import org.openqa.selenium.TimeoutException;public boolean isValid(WebElement e) { try { WebElementWait wait = new WebElementWait(e, 1); //@SuppressWarnings("unused") //WebElement icon = wait.until(new Function<WebElement, WebElement>() { public WebElement apply(WebElement d) { return d.findElement(By .xpath("./following-sibling::div[]")); } }); return false; } catch (TimeoutException exception) { return true; }}


