我正在使用for循环遍历动态列表,当我必须在其中执行Catch时,会跳过一些选项。
for (int index = 0; index < exports.Rows.Count; index++)
{
try
{
var reportListBox = driver.FindElement(By.ClassName("rlbList"));
IWebElement reportItem = reportListBox.FindElement(By.CssSelector($"#ctl00_ContentPlaceHolder_lstReports_i{index}"));
reportItem.Click();
exportBTNClick();
}
catch (Exception)
{
// If Error Message appears then Click ok
driver.FindElement(By.XPath("//span[@class='rwInnerSpan'][contains(text(),'OK')]")).Click();
driver.FindElement(By.XPath("//span[@class='rwInnerSpan'][contains(text(),'OK')]")).Click();
}
}
代码显示For循环并将从列表中收集信息,然后reportItem.Click();
的目的是单击屏幕上显示的选项。
exportBTNClick();
方法将单击屏幕上的按钮并生成报告。如果无法生成报告(即没有有效数据),则应移动到Catch,然后单击“确定”按钮两次。
然后发生的是点击OK按钮并导出下一个报告比最后一个选择提前2或3个位置(即如果报告2失败应该转到报告3,而是报告4或5)
发现问题是正在调用导出方法,然后没有数据的错误消息可用。由于错误消息仅出现在序列中的下一个项目,因此从上次导出时屏幕上出现错误消息,它跳转到语句的catch部分。
新代码如下所示:
for (int index = 0; index < exports.Rows.Count; index++)
{
var reportListBox = driver.FindElement(By.ClassName("rlbList"));
IWebElement reportItem = reportListBox.FindElement(By.CssSelector($"#ctl00_ContentPlaceHolder_lstReports_i{index}"));
reportItem.Click();
exportBTNClick();
IWebElement modal = driver.FindElement(By.XPath("//span[@class='rwInnerSpan'][contains(text(),'OK')]"));
if (modal.Displayed == true)
{
// If Error Message appears then Click ok
driver.FindElement(By.XPath("//span[@class='rwInnerSpan'][contains(text(),'OK')]")).Click();
driver.FindElement(By.XPath("//span[@class='rwInnerSpan'][contains(text(),'OK')]")).Click();
}
}