Webdriver: How to click on a button for a specific

2019-09-19 12:56发布

table data

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.

2条回答
聊天终结者
2楼-- · 2019-09-19 13:41

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;
    }

   }
  }
}
查看更多
女痞
3楼-- · 2019-09-19 13:50

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.

查看更多
登录 后发表回答