Selenium 等待.直到(ExpectedCondition)生成错误
升级到selenium Java 3.8.1后,Selenium 等待.直到(ExpectedCondition)生成错误,selenium,selenium-webdriver,wait,Selenium,Selenium Webdriver,Wait,升级到selenium Java 3.8.1后,wait.until(ExpectedCondition)开始发出错误消息 对于下面的代码段 WebElement framei = wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath(".//*[@id='ctl00_ContentPlaceHolder1_dlgModal_IFrame']"))); driver.switchTo().fram
wait.until(ExpectedCondition)
开始发出错误消息
对于下面的代码段
WebElement framei = wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath(".//*[@id='ctl00_ContentPlaceHolder1_dlgModal_IFrame']")));
driver.switchTo().frame(framei);
WebElement AcceptRadioButton=wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//*[@id='RblStatus']/tbody/tr[1]/td/label")));
AcceptRadioButton.click();
给出了以下错误:
Type The method until(Function<? super WebDriver,V>) in the type FluentWait<WebDriver> is not applicable for the arguments (ExpectedCondition<WebElement>)
键入方法直到(函数,如错误消息中所述:
FluentWait<WebDriver> is not applicable for the arguments (ExpectedCondition<WebElement>)
其中函数
为:
public interface Function<T, R>
公共接口功能
因此,它已转换为使用Java 8功能接口。您需要相应地重写您的预期条件。根据最佳实践,我们必须尝试使用适当的WebDriverWait
切换到
,如下所示:
new WebDriverWait(driver, 10).until(ExpectedConditions.frameToBeAvailableAndSwitchToIt(By.xpath("//iframe[@id='ctl00_ContentPlaceHolder1_dlgModal_IFrame']")));
与您的问题相同,但对Eugene的答案不太确定。我在selenium java 2.53.1和3.8.1的源代码中搜索,以了解FluentWait类之间的差异。以下是不同版本的功能:
2.53.1:
public void until(final Predicate<T> isTrue) {
until(new Function<T, Boolean>() {
public Boolean apply(T input) {
return isTrue.apply(input);
}
public String toString() {
return isTrue.toString();
}
});
}
public void until(最终谓词为true){
直到(新函数(){
公共布尔应用(T输入){
返回isTrue.apply(输入);
}
公共字符串toString(){
return为true.toString();
}
});
}
或
public V-until(函数
public void until(final Predicate<T> isTrue) {
until(new Function<T, Boolean>() {
public Boolean apply(T input) {
return isTrue.apply(input);
}
public String toString() {
return isTrue.toString();
}
});
}
public <V> V until(Function<? super T, V> isTrue) {
long end = clock.laterBy(timeout.in(MILLISECONDS));
Throwable lastException = null;
while (true) {
try {
V value = isTrue.apply(input);
if (value != null && Boolean.class.equals(value.getClass())) {
if (Boolean.TRUE.equals(value)) {
return value;
}
} else if (value != null) {
return value;
}
} catch (Throwable e) {
lastException = propagateIfNotIgnored(e);
}
// Check the timeout after evaluating the function to ensure conditions
// with a zero timeout can succeed.
if (!clock.isNowBefore(end)) {
String message = messageSupplier != null ?
messageSupplier.get() : null;
String toAppend = message == null ?
" waiting for " + isTrue.toString() : ": " + message;
String timeoutMessage = String.format("Timed out after %d seconds%s",
timeout.in(SECONDS), toAppend);
throw timeoutException(timeoutMessage, lastException);
}
try {
sleeper.sleep(interval);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
throw new WebDriverException(e);
}
}
}
public <V> V until(Function<? super T, V> isTrue) {
long end = clock.laterBy(timeout.in(MILLISECONDS));
Throwable lastException;
while (true) {
try {
V value = isTrue.apply(input);
if (value != null && (Boolean.class != value.getClass() || Boolean.TRUE.equals(value))) {
return value;
}
// Clear the last exception; if another retry or timeout exception would
// be caused by a false or null value, the last exception is not the
// cause of the timeout.
lastException = null;
} catch (Throwable e) {
lastException = propagateIfNotIgnored(e);
}
// Check the timeout after evaluating the function to ensure conditions
// with a zero timeout can succeed.
if (!clock.isNowBefore(end)) {
String message = messageSupplier != null ?
messageSupplier.get() : null;
String timeoutMessage = String.format(
"Expected condition failed: %s (tried for %d second(s) with %s interval)",
message == null ? "waiting for " + isTrue : message,
timeout.in(SECONDS), interval);
throw timeoutException(timeoutMessage, lastException);
}
try {
sleeper.sleep(interval);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
throw new WebDriverException(e);
}
}
}