Webdriver: How to click on a button for a specific row of a table (C#)

﹥>﹥吖頭↗ 提交于 2019-12-25 18:38:13

问题


Hi,

The table shown in the picture is dynamic. The test case is to click on the respective Delete button where the name is equal to "Test Group 2".

Please suggest C# code.


回答1:


Not knowing what is under that fourth td I can't say for sure, but you can find it with an xpath that looks something like this.

//td[contains(text(),'Test Group 2')]/..//td[4]//button

You might have to specify which button since that edit button will be in the same td.




回答2:


You can use the below method. Pass the value for what you are looking for like "Test Group 2". The below will loop through the table and stop on the value. From there, pass in the below to click the trash can.

 tds[i + 4].Click();

So look at the columns and just count to the right (+) or left (-). If you wanted to click the assign button it should be:

tds[i + 2].Click();

If you had a button to the left of "Test Group 2" you would pass in:

tds[i - 1].Click();

Method:

 public void ClickTableLink(string value)
 {
 var table =  driver.FindElement(By.Id("assetGroup-table"));
 foreach (var tr in table.FindElements(By.TagName("tr")))
 {
 var tds = tr.FindElements(By.TagName("td"));
for (var i = 0; i < tds.Count; i++)
{
    if (tds[i].Text.Trim().Contains(value))
    {
        tds[i + 4].Click();
        break;
    }

   }
  }
}


来源:https://stackoverflow.com/questions/43853855/webdriver-how-to-click-on-a-button-for-a-specific-row-of-a-table-c

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!