我需要等待 javascript 的结果与字符串或 boolean 值匹配。
使用这个 JavaScript:
document.getElementById('video_html5').seeking;
我得到一个“false”/“true”值,并且我需要等到该值为“false”,所以我确定视频没有在寻找,但我只找到了等待的方法javascript 命令值,而不是检查该值是否与某些文本匹配的方法。
new WebDriverWait(driver, 30)
.until(ExpectedConditions.jsReturnsValue("return document.getElementById('video_html5').seeking;"));
因为在某些情况下我会得到一个 boolean 值以外的字符串,并且需要比较这些字符串。
我已经找到了如何在 Ruby 中做到这一点,但在 Java 中却没有:
wait_element = @wait.until { @driver.execute_script("return document.getElementById('vid1_html5_api_Shaka_api').seeking;").eql? false }
最佳答案
您可以编写自己的自定义预期条件。
public class MyCustomConditions {
public static ExpectedCondition<Boolean> myCustomCondition() {
return new ExpectedCondition<Boolean>() {
@Override
public Boolean apply(WebDriver driver) {
return (Boolean) ((JavascriptExecutor) driver)
.executeScript("return document.getElementById('video_html5').seeking === 'string_value' || ... ");
}
};
}
}
然后在您的测试中,您可以按如下方式使用条件。
WebDriverWait wait = new WebDriverWait(driver, 30);
wait.until(MyCustomConditions.myCustomCondition());
关于javascript - Selenium - 等待返回的 javascript 脚本值匹配值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41554625/