1

我一直在尝试解决我面临的问题,但我没有想法,所以我需要帮助。

我有一个非常简单的页面:

╔════════════════════════════╦═══════════════════════════════════════╗
║          RoleName          ║                Delete                 ║
╠════════════════════════════╬═══════════════════════════════════════╣
║ ABC1 (index starts with 1) ║ [delete button] (index starts with 0) ║
║ ABC2                       ║ [delete button]                       ║
║ ABC3                       ║ [delete button]                       ║
║ ABC4                       ║ [delete button]                       ║
║ ABC5                       ║ [delete button]                       ║
╚════════════════════════════╩═══════════════════════════════════════╝

这是自动化它的代码:

//count how many rows avaiable
IList<IWebElement> count = driver.FindElements(By.CssSelector("div#ctl00_MainContent_divUserInfo"));

//note: starting with 1 since the RoleName colum starts with 1 (html code render on the page)
for (int i = 1; i <= count.Count; i++)
{

    string selName = string.Empty;
    selName = String.Format("div#ctl00_MainContent_divUserInfo tr.item:nth-of-type({0}) > td a#aDetail:nth-child(1)", i);

    string selNameText = TextByCssSelector(selName);
    bool isNameExists = Names.Any(s => selNameText.Equals(s.Value.ToString(), StringComparison.OrdinalIgnoreCase));

    if (isNameExists)
    {
        //if found then click on delete button to remove the item....
    }

}

上面的代码按我预期的那样工作,但是有一个问题,问题是如果我删除该行而不是发生以下事情:

  1. 页面重新渲染页面
  2. 我得到了一组新的计数

但请记住,我仍在 中for-loop,所以我的计数是基于以前的(在我删除该行之前)我的问题是:

我应该如何处理这种情况?

4

2 回答 2

3

以相反的顺序运行循环:

for (int i = count.Count; i > 0; i--) { ... }
于 2013-05-07T20:53:42.390 回答
3

既然您基本上想删除所有行,直到没有剩余的行可以删除,为什么不重新设计循环以考虑到这一点,如下所示:

string selName = "//div[@id='ctl00_MainContent_divUserInfo']//tr[contains(@class, 'item')]/td//a[@id='aDetail'][1]";
while(driver.FindElements(By.CssSelector("div#ctl00_MainContent_divUserInfo")).Count > 0){
    string selNameText = TextByCssSelector(selName);
    bool isNameExists = Names.Any(s => selNameText.Equals(s.Value.ToString(), StringComparison.OrdinalIgnoreCase));

    if (isNameExists)
    {
        //if found then click on delete button to remove the item....
    }

}
于 2013-05-08T03:01:37.480 回答