簡體   English   中英

如何在Selenium Webdriver中等待輸入字段刷新

[英]How to wait for input field to refresh in Selenium webdriver

在我的AUT中,有兩個字段- 'Product'下拉列表和'Amount'輸入字段。 默認情況下, 'Amount'字段顯示值'0.0' 當用戶從'Product'下拉列表中選擇一種產品時,如果價格已經在數據庫中可用, 'Amount'字段將自動填充所選產品的價格,否則, 'Amount'字段將顯示'0.0' 選擇產品后,需要一段時間才能將金額加載到'Amount'字段中。 我無法觀察到'Amount'字段的屬性值在自動填充之前和之后的任何變化。 'Amount'字段的html是

<input id="id_expense_amt" class="form-control input" name="expense_amt" step="0.01" value="0" type="number">

問題是選擇產品后,如何使網絡驅動程序等待刷新金額字段。 我使用了Thread.sleep() ,它工作正常。 但是還有其他可用方法嗎?

使用WebDriverWait基本上有兩種方法可以實現此方案,如下所示:

WebDriverWait wait = new WebDriverWait(driver, 10); 
  • 如果從Product下拉列表中選擇一個選項后,如果您已經知道“ Amount文本字段中的Amount將從默認金額更改為多少,那么您應該嘗試使用ExpectedConditions.textToBePresentInElementValue ,它將等待直到指定的元素值屬性中存在給定的金額,如下所示: -

     webDriverWait.until(ExpectedConditions.textToBePresentInElementValue(By.id("id_expense_amt"), "Amount which would be change")); 
  • 如果您不知道從“ Product下拉列表中選擇一個選項后,“ Amount文本字段中的Amount將從默認金額更改為多少,但是您知道默認金額為0 ,那么您需要創建自定義的ExpectedConditions ,它將等到金額為從默認值0更改為以下任何新值:-

     wait.until(new ExpectedCondition<Boolean>() { public Boolean apply(WebDriver d) { WebElement el = d.findElement(By.id("id_expense_amt")); String value = el.getAttribute('value'); if(value.length() != 0 && !value.equals("0")) { return true; } } }); 

如果希望使用下面的方法,則可以根據需要更改時間單位。

 driver.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS);

您也可以使用這個

WebDriverWait wait = new WebDriverWait(getWebDriver(), 10);

WebElement element = wait.until(ExpectedConditions.visibilityOf(element));

如果您知道應在金額文本框中填充的確切值,則可以使用ExpectedConditions.textToBePresentInEl‌ement

WebDriverWait wait = new WebDriverWait(driver, 60); 
webDriverWait.until(ExpectedConditions.textToBePresentInEl‌ement(By.xpath("text‌​box xpath"), "text for which you are waiting"));

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM