【发布时间】:2019-02-19 13:51:31
【问题描述】:
我的应用程序中有两个不同的表单,表单输入数据加载可以使用 UI 中可用的预定义模板完成(即输入文本字段值将根据模板选择预先填充)。当数据加载发生时,元素渲染不会发生,只会填充输入字段值。为了确认数据加载,我使用如下等待
var wait = new WebDriverWait(_driver, TimeSpan.FromSeconds(60));
wait.Until(x => x.FindElement(By.XPath(Element1_FieldXPath)).GetAttribute("value").Length > 1);
在任何时候,Form1 或 Form2 都是可见的。因此,我需要确认 Form1 或 Form2 中的数据加载。所以,我需要编写一个通用方法来处理这种情况,我遇到了以下解决方案,
public bool IsDataLoadingCompleted()
{
var wait = new WebDriverWait(_driver, TimeSpan.FromSeconds(60));
try
{
wait.Until(x => x.FindElement(By.XPath(Form1_FieldXPath)).GetAttribute("value").Length > 1);
return true;
}
catch (TimeoutException)
{
try
{
wait.Until(x => x.FindElement(By.XPath(Form2_FieldXPath)).GetAttribute("value").Length > 1);
return true;
}
catch (TimeoutException)
{
return false;
}
}
}
在我的解决方案中,当 form2 在 UI 中可见时,最坏情况的等待时间是 120s。我需要用单等待而不是双等待来解决上述问题。
有没有其他方法可以用最少的等待时间解决问题?
示例 HTML:
Form1 - Field1 HTML:
<div class="form-group field field-string">
<label class="control-label" for="root_employeeName">Employee name</label>
<input class="form-control" id="root_employeeName" label="Employee name" placeholder="" type="text" value="">
</div>
Form2 - Field1 HTML:
<div class="form-group field field-string">
<label class="control-label" for="root_employeeAddress">Employee Address</label>
<input class="form-control" id="root_employeeAddress" label="Employee Address" placeholder="" type="text" value="">
</div>
【问题讨论】:
-
答案取决于这些表格的定位器。你能分享表单的html代码吗?
-
@theGuy:问题中添加了示例 HTML
标签: c# selenium selenium-webdriver css-selectors webdriverwait