首页 > 解决方案 > 如何使用 selenium 和 c# 获取表中的内表数据

问题描述

我想计算外部表的数量。这是我到目前为止所尝试的。在 HTML 中,我删除了一些部分以减少代码的长度。

var reports =
  driver.FindElements(By.Id("Outer Table"));
var formss = new List < object > ();
foreach(var item in reports) {
  formss.Add(item);
}
<div id="Outer Table" onclick="OnTreeClick(event)">
  <table cellpadding="0" cellspacing="0" style="border-width:0;">
    <tr>
      <td style="white-space:nowrap;">
        <a class="VMIContentPanelHolder_TreeViewSiteSection_0">Fix Auto Leeds (TF 
    Smiths)</a>
      </td>
    </tr>
  </table>
  <table cellpadding="0" cellspacing="0" style="border-width:0;">
    <tr>
      <td style="white-space:nowrap;">
        <a class="VMIContentPanelHolder_TreeViewSiteSection_0">Fix Auto Bradford (TF 
    Smiths)</a>
      </td>
    </tr>
  </table>
</div>

标签: c#htmlselenium

解决方案


整理出了问题。这里有一些重要的文章>>
1) http://toolsqa.com/selenium-webdriver/c-sharp/handle-dynamic-webtables-with-selenium-in-csharp/
2) http://toolsqa.com/selenium-网络驱动程序/定位器/

IWebElement TableDiv = driver.FindElement(By.CssSelector("#VMIContentPanelHolder_TreeViewSiteSection"));
List<IWebElement> TableTagList = new List<IWebElement>(TableDiv.FindElements(By.TagName("table")));
var reportNameList = new List<string>();
foreach (var TableTag in TableTagList)
 {
  reportNameList.Add(TableTag.FindElement(By.TagName("a")).Text.ToString());
  }
properties.reportCount= reportNameList.Count();
var isListsDiffer = properties.reportList.Except(reportNameList).ToList();
properties.isReportsMatched = !isListsDiffer.Any();
properties.displayValues(properties);

谢谢你!!


推荐阅读