Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/289.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何枚举表的第三列_C#_Selenium_Xpath - Fatal编程技术网

C# 如何枚举表的第三列

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,

我的表有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,我就知道最后一个索引和文本


我将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语言中的元素