【问题标题】:Selenium Wait for AJAX edit to text on pageSelenium 等待 AJAX 编辑页面上的文本
【发布时间】:2019-08-17 22:55:30
【问题描述】:

我的网页上有一个元素在页面加载后几乎立即由 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...
    }

【问题讨论】:

  • 编辑页面上的文本的代码试验?你看到了什么错误?
  • 超时异常

标签: java ajax selenium webdriverwait


【解决方案1】:

尝试使用 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...

【讨论】:

  • 这个解决方案对我有用,虽然我不明白,而且我自己也不会来这里。传递的 "and contains(text(),'" +newBalance +"')" 对我来说毫无意义。
【解决方案2】:

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

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

【讨论】:

  • 除了允许我设置轮询时间之外,这与使用 wait.until 有何不同?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-03-20
  • 2017-03-27
  • 1970-01-01
  • 2011-08-17
  • 1970-01-01
  • 2017-11-10
  • 2018-06-14
相关资源
最近更新 更多