我可以读取表值,但无法将值添加到foreach循环中,并将它们作为List<string>返回。
请分享你的方法,这将是非常有帮助的
IWebElement tableElement = driver.FindElement(By.XPath("//div[@id='table']"));
IList<IWebElement> tableRow = tableElement.FindElements(By.TagName("tr"));
IList<IWebElement> rowTD;
foreach (IWebElement row in tableRow)
{
rowTD = row.FindElements(By.TagName("td"));
//add the values in list??
}
return list在此之后,如何将td文本值添加到List<string>中?我想让这个函数返回列表。
发布于 2019-02-07 00:00:01
您可以将Michiel's answer与LINQ结合使用,以更简洁地完成这项工作--根本不需要foreach循环:
首先,添加
using System.Linq;然后,您可以将整个代码块替换为:
return driver.FindElement(By.XPath("//div[@id='table']"))
.FindElements(By.TagName("td"))
.Select(e => e.Text)
.ToList();https://stackoverflow.com/questions/54556837
复制相似问题