繁体   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