我能读表中的值,但林无法添加值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>
?我想这个函数返回列表中。
我猜你正在试图返回所有表数据元素作为字符串列表。如果你想坚持一个类似的只获得了TD元素的文本的方法,那么我会做这样的事情:
IWebElement tableElement = driver.FindElement(By.XPath("//div[@id='table']"));
IList<IWebElement> tableDataElements = tableElement.FindElements(By.TagName("td"));
var reults = new List<string>();
foreach (IWebElement tableDataElement in tableDataElements)
{
var tableData = tableDataElement.Text;
reults.add(tableData);
}
return reults;
你可以结合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();