我需要在每次更新后验证一个元素.Ex:低温:7.54经过一段时间后,该值变为Lowrtemperature:3.78,5.87,9.32 ......等等,每次经过一段时间(时间变化)后,值会发生变化。但我需要驱动程序在更改后获取更新的值。如何使用selenium webdriver获取元素
WebElement PrecipitationLabel_14_L= (new WebDriverWait(driver, 30)).until(ExpectedConditions.presenceOfElementLocated(By.xpath(".//*[@id='webFor']/div/div[1]/div[1]/div/div[2]")));
String Precipationclass= PrecipitationLabel_14_L.getAttribute("class");
return Precipationclass;
答案 0 :(得分:1)
测试的目的应该是确保文本更新。 是否重新加载页面应该与此无关。
以下是等待文本更新的示例:
WebDriverWait wait = new WebDriverWait(driver, 30);
String temperatureBefore = wait.until(textToBeDifferent(By.cssSelector(...), ""));
String temperatureAfter = wait.until(textToBeDifferent(By.cssSelector(...), temperatureBefore));
定制服务员:
public static ExpectedCondition<String> textToBeDifferent(final By locator, final String text) {
return new ExpectedCondition<String>() {
@Override
public String apply(WebDriver driver) {
try {
String elemText = driver.findElement(locator).getText().trim();
return elemText.equals(text) ? null : elemText;
} catch (NoSuchElementException e) {
return null;
} catch (StaleElementReferenceException e) {
return null;
}
}
@Override
public String toString() {
return String.format("text ('%s') to not be found by %s", text, locator);
}
};
}