问题
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