C# 如何枚举表的第三列
我的表有3列。我想使用Selenium.IE获取第3列中的元素。但是我不知道有多少行。我保证至少有一行C# 如何枚举表的第三列,c#,selenium,xpath,C#,Selenium,Xpath,我的表有3列。我想使用Selenium.IE获取第3列中的元素。但是我不知道有多少行。我保证至少有一行 [@id="content"]/table/tbody/tr[1]/th[3]/span [@id="content"]/table/tbody/tr[2]/td[3]/span [@id="content"]/table/tbody/tr[3]/td[3]/span 我的Xpath与上面的类似。查找所有行中的所有元素的推荐方法是什么?我应该使用try-catch吗?一旦找不到Xpath,
[@id="content"]/table/tbody/tr[1]/th[3]/span
[@id="content"]/table/tbody/tr[2]/td[3]/span
[@id="content"]/table/tbody/tr[3]/td[3]/span
我的Xpath与上面的类似。查找所有行中的所有元素的推荐方法是什么?我应该使用try-catch吗?一旦找不到Xpath,我就知道最后一个索引和文本
我将C与InternetExplorerDriverse服务一起使用使用@har07提到的Xpath
[@id="content"]/table/tbody/tr/td[3]/span
您可以使用获取与xpath匹配的所有元素并对其进行迭代
ReadOnlyCollection<IWebElement> elements= driver.FindElements(By.XPath("[@id='content']/table/tbody/tr/td[3]/span"));
foreach(IWebElement element in elements)
{
//do whatever with the element
}
生成一个泛型函数以获取表并添加逻辑以获取行列表以及遍历行中单元格的能力 现在,您可以创建另一个列表来存储列的数据,方法是通过选择并存储每行的特定单元格值来遍历该行直到表的末尾 返回列表将存储列的值 范例
WebElement dataTable= YOUR Table Selector ( i.e Driver.findElement By your Element Identifier);
List<WebElement> allRows= dataTable.findElements(By.tagName("tr"));
for(int rowCount=0; rowCount< allRows.size();rowCount++){
List<WebElement> allCells= allRows.get(rowCount).findElements(By.tagName("td"));
Now We can get Specific CELL Value ( In your case the CELL value will always 3 if you want to Store the 3rd column data)
Iterate and store the cell value from each row to a New List
不指定索引,比如[@id=content]/table/tbody/tr/td[3]/span,怎么样?这将返回您可以迭代的所有行。您能告诉我迭代将如何完成吗?请在文档中查找xpath解析器的示例。通常,它是一个列表/数组,我不熟悉C语言中的元素