我的网页上有一个元素,在页面加载后几乎立即通过 AJAX 更新。我知道我期望的变化是什么,并希望 Selenium 等待变化并捕获它。我正在尝试使用显式等待。但是,我收到了 timeoutException,因为 Selenium 没有检测到更改。
我知道我正确选择了元素和值,因为我使用了打印语句。我已经使用
解决了这个问题Java Thead.sleep(1000)
然后使用
driver.findElement(By.id("balance-sms")).getText()
但这不是一个可接受的解决方案。
private void modalSend(String newBalence){
WebDriverWait wait = new WebDriverWait(driver, 10);
wait.until(ExpectedConditions.elementToBeClickable(modalSendButton)); //this wait works fine
modalSendButton.click(); //this results in a page refresh
//now check for the AJAX change to this element...normally takes about 1 second
wait.until(ExpectedConditions.textToBePresentInElement(driver.findElement(By.id("balance-sms")),newBalence));
//continue...
}
最佳答案
尝试使用 xpath
和 visibilityOfElementLocated
进行定位器并进行一些修改:
//now check for the AJAX change to this element...normally takes about 1 second
wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//*[@id='balance-sms' and contains(text(),'" +newBalence +"')]")));
//continue...
关于java - Selenium 等待 AJAX 编辑页面上的文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57540492/