Selenium 等待 AJAX 编辑页面上的文本

Selenium Wait for AJAX edit to text on page

我的网页上有一个元素 AJAX 几乎在页面加载后立即更新。我知道我期望更改是什么,并希望 Selenium 等待更改并捕获它。我正在尝试为此使用显式等待。但是,由于 Selenium 未检测到更改,我收到 timeoutException。

我知道我正确选择了元素和值,因为我使用了打印语句。我已经使用

解决了这个问题

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...
    }

尝试使用 xpathvisibilityOfElementLocated 的定位器并进行一些修改:

//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...

尝试像下面这样流畅地等待,而不是 Thread.sleep

Wait wait = new FluentWait(WebDriver reference)
.withTimeout(timeout, SECONDS)
.pollingEvery(timeout, SECONDS)
.ignoring(Exception.class);