如何使用 selenium C# 访问 HTML table

How to access HTML table using selenium C#

我正在尝试使用 selenium c# 网络驱动程序访问 html。

我的部分html如下(Full html is here).

<table scope="band" type="tabular" cellpadding="2" border="0" bgcolor="#555555" id="bandstyle-Ver2" cellspacing="1">
<tbody>
  <tr bandheader="true" type="table-header" height="22px">
     <td label-type="text" height="1%" width="2%" class="textbrownNew">No</td>
     <td label-type="text" height="1%" width="3%" class="textbrownNew">Inst Catg</td>
     <td label-type="text" height="1%" width="5%" class="textbrownNew">Inst - Branch</td>
     <td label-type="text" height="1%" width="3%" class="textbrownNew">CF Type</td>
     <td label-type="text" height="1%" width="3%" class="textbrownNew">CF Stat</td>
     <td label-type="text" height="1%" width="3%" class="textbrownNew">Own - Shp</td>
     <td label-type="text" height="1%" width="3%" class="textbrownNew">Crcy</td>
     <td label-type="text" height="1%" width="7.5%" class="textbrownNew">Amount Granted / Limit</td>
     <td label-type="text" height="1%" width="7.5%" class="textbrownNew">Current Balance</td>
     <td label-type="text" height="1%" width="7.5%" class="textbrownNew">Arrears Amount</td>
     <td label-type="text" height="1%" width="7.5%" class="textbrownNew">Installment Amount</td>
     <td label-type="text" height="1%" width="7.5%" class="textbrownNew">Amount Written Off</td>
     <td label-type="text" height="1%" width="6%" class="textbrownNew">Reported Date</td>
     <td label-type="text" height="1%" width="6%" class="textbrownNew">First Disburse Date</td>
     <td label-type="text" height="1%" width="6%" class="textbrownNew">Latest Payment Date</td>
     <td label-type="text" height="1%" width="6%" class="textbrownNew">Restruct Date</td>
     <td label-type="text" height="1%" width="6%" class="textbrownNew"> End  Date </td>
     <td label-type="text" height="1%" width="3%" class="textbrownNew">Repay Type</td>
     <td label-type="text" height="1%" width="4%" class="textbrownNew">Purp.</td>
     <td label-type="text" height="1%" width="4%" class="textbrownNew">Coverage</td>
  </tr>
  <tr height="22px">
     <td height="1%" width="2%" class="text2New" bgcolor="#FFFFFF">1</td>
     <td height="1%" width="3%" class="text2New" bgcolor="#FFFFFF">CBC</td>
     <td height="1%" width="5%" class="text2New" bgcolor="#FFFFFF">OWN-072</td>
     <td height="1%" width="3%" class="text2New" bgcolor="#FFFFFF">LOAN</td>
     <td height="1%" width="3%" class="text2New" bgcolor="#FFFFFF">ACTV</td>
     <td height="1%" width="3%" class="text2New" bgcolor="#FFFFFF">OWN</td>
     <td height="1%" width="3%" class="text2New" bgcolor="#FFFFFF">LKR</td>
     <td height="1%" width="7.5%" align="right" class="text2New" bgcolor="#FFFFFF">4,000,000</td>
     <td height="1%" width="7.5%" align="right" class="text2New" bgcolor="#FFFFFF">3,731,292</td>
     <td height="1%" width="7.5%" align="right" class="text2New" bgcolor="#FFFFFF">0</td>
     <td height="1%" width="7.5%" align="right" class="text2New" bgcolor="#FFFFFF">94,290</td>
     <td height="1%" width="7.5%" align="right" class="text2New" bgcolor="#FFFFFF"><img src="./CRIB Report Number _ W-0043111705_2018_files/spacer.gif"></td>
     <td height="1%" width="6%" class="text2New" bgcolor="#FFFFFF">30-Jun-2018</td>
     <td height="1%" width="6%" class="text2New" bgcolor="#FFFFFF">14-Feb-2018</td>
     <td height="1%" width="6%" class="text2New" bgcolor="#FFFFFF"><img src="./CRIB Report Number _ W-0043111705_2018_files/spacer.gif"></td>
     <td height="1%" width="6%" class="text2New" bgcolor="#FFFFFF"><img src="./CRIB Report Number _ W-0043111705_2018_files/spacer.gif"></td>
     <td height="1%" width="6%" class="text2New" bgcolor="#FFFFFF"><img src="./CRIB Report Number _ W-0043111705_2018_files/spacer.gif"></td>
     <td height="1%" width="3%" class="text2New" bgcolor="#FFFFFF">MNLY</td>
     <td height="1%" width="4%" class="text2New" bgcolor="#FFFFFF">01:001</td>
     <td height="1%" width="4%" class="text2New" bgcolor="#FFFFFF">FS &amp; OG</td>
  </tr>
  <tr height="22px">
     <td height="1%" width="2%" class="text2New" bgcolor="#FFFFFF">2</td>
     <td height="1%" width="3%" class="text2New" bgcolor="#FFFFFF">CBC</td>
     <td height="1%" width="5%" class="text2New" bgcolor="#FFFFFF"><img src="./CRIB Report Number _ W-0043111705_2018_files/spacer.gif"></td>
     <td height="1%" width="3%" class="text2New" bgcolor="#FFFFFF">CRCD</td>
     <td height="1%" width="3%" class="text2New" bgcolor="#FFFFFF">ACTV</td>
     <td height="1%" width="3%" class="text2New" bgcolor="#FFFFFF">OWN</td>
     <td height="1%" width="3%" class="text2New" bgcolor="#FFFFFF">LKR</td>
     <td height="1%" width="7.5%" align="right" class="text2New" bgcolor="#FFFFFF">800,000</td>
     <td height="1%" width="7.5%" align="right" class="text2New" bgcolor="#FFFFFF">0</td>
     <td height="1%" width="7.5%" align="right" class="text2New" bgcolor="#FFFFFF">0</td>
     <td height="1%" width="7.5%" align="right" class="text2New" bgcolor="#FFFFFF"><img src="./CRIB Report Number _ W-0043111705_2018_files/spacer.gif"></td>
     <td height="1%" width="7.5%" align="right" class="text2New" bgcolor="#FFFFFF"><img src="./CRIB Report Number _ W-0043111705_2018_files/spacer.gif"></td>
     <td height="1%" width="6%" class="text2New" bgcolor="#FFFFFF">31-Jul-2018</td>
     <td height="1%" width="6%" class="text2New" bgcolor="#FFFFFF">25-Apr-2018</td>
     <td height="1%" width="6%" class="text2New" bgcolor="#FFFFFF"><img src="./CRIB Report Number _ W-0043111705_2018_files/spacer.gif"></td>
     <td height="1%" width="6%" class="text2New" bgcolor="#FFFFFF"><img src="./CRIB Report Number _ W-0043111705_2018_files/spacer.gif"></td>
     <td height="1%" width="6%" class="text2New" bgcolor="#FFFFFF"><img src="./CRIB Report Number _ W-0043111705_2018_files/spacer.gif"></td>
     <td height="1%" width="3%" class="text2New" bgcolor="#FFFFFF">DMND</td>
     <td height="1%" width="4%" class="text2New" bgcolor="#FFFFFF">03:001</td>
     <td height="1%" width="4%" class="text2New" bgcolor="#FFFFFF">US &amp; NG</td>
  </tr>
</tbody>
</table>

在我的 html 中有多个内部 table。 full html code 要访问此 html 文件,我使用了以下 Selenium 代码。

var table = driver.FindElement(By.Id("reportcontainerstyle-Ver2"));
var rows = table.FindElements(By.TagName("tr"));

//To get days arrears details
var mainTable = driver.FindElement(By.Name("ConsumerCreditDetails_Version3"));
var subTables = mainTable.FindElements(By.Id("bandstyle-Ver2"));

for (int i = 0; i < rows.Count; i++)
{
    if (i != 0)//Skip header
    {
        var row = rows[i];
        var rowTds = row.FindElements(By.TagName("td"));

        if ((rowTds[3].Text.Trim().Equals("LOAN") || rowTds[3].Text.Trim().Equals("LEAS") || rowTds[3].Text.Trim().Equals("CRCD") || rowTds[3].Text.Trim().Equals("OVDR")) && rowTds[4].Text.Trim().Equals("ACTV") && rowTds[5].Text.Trim().Equals("OWN"))
        //if(true)
        {

        }

    }
} 

在 HTML 文件中,有 tables with id = bandstyle-Ver2 我需要访问第二个 table 的数据。为了实现这一点,我使用了上面的硒代码。但是当我像那个代码一样访问 table 时。发生此错误。

Index was out of range. Must be non-negative and less than the size of the collection. Parameter name: index

谁能告诉我如何访问这些数据。

更新:根据@sers 的回答,我使用了以下代码:

    var rows = driver.FindElements(By.XPath("//table[.//td[normalize-space(.)='Credit Facility (CF) Details']][1]/following-sibling::table[1]//tr[not(@type='table-header')]"));

    foreach (IWebElement row in rows)
    {
        var cfType = row.FindElement(By.XPath("td[4]")).Text.Trim();
        var cfStat = row.FindElement(By.XPath("td[5]")).Text.Trim();

        if ((cfType.Equals("LOAN") || cfType.Equals("LEAS") || cfType.Equals("CRCD") || cfType.Equals("OVDR")) && cfStat.Equals("ACTV") && cfStat.Equals("OWN"))
        {

        }
    }

那我也有问题。我需要访问这些 table。这是 HTML part 在这个 table 中,我需要获得每个 table 的最大数量,如果该数字 > 90 则有逻辑。为此,我使用了以下代码。

for (int i = 0; i < rows.Count; i++)
{
    if (i != 0)//Skip header
    {
        var row = rows[i];
        var rowTds = row.FindElements(By.TagName("td"));

        if ((rowTds[3].Text.Trim().Equals("LOAN") || rowTds[3].Text.Trim().Equals("LEAS") || rowTds[3].Text.Trim().Equals("CRCD") || rowTds[3].Text.Trim().Equals("OVDR")) && rowTds[4].Text.Trim().Equals("ACTV") && rowTds[5].Text.Trim().Equals("OWN"))
        {
            for (int subTab = 0; subTab < subTables.Count; subTab++)
            {
                if (subTab > 1)
                {
                    var colsDaysArrers = subTables[subTab].FindElements(By.TagName("td"));
                    if (colsDaysArrers.Count > 0)
                    {
                        if (colsDaysArrers[0].Text.Equals(crib.NO.ToString()))
                        {
                            for (int j = 1; j <= colsDaysArrers.Count - 1; j++)
                            {

                                var text = colsDaysArrers[j].Text;
                                if (!(text.Equals("--") || text.Equals("OK") || text.Equals("")))
                                {
                                    var val = double.Parse(text);
                                    if (val > 90)
                                    {
                                        crib.FACILITYARREARSSTATUS = "Irrigular";
                                        break;
                                    }
                                    else
                                    {
                                        crib.FACILITYARREARSSTATUS = "Regular";
                                    }

                                }
                            }

                            break;

                        }
                    }
                }
            }

        }

    }
}

请告诉我这段代码是否适合执行此操作?或者还有其他办法吗?

第一个 "Credit Facility (CF) Details" table:

driver.FindElement(By.Xpath("//table[.//td[normalize-space(.)='Credit Facility (CF) Details']][1]/following-sibling::table[1]"));

第二个"Credit Facility (CF) Details"table:

driver.FindElement(By.Xpath("//table[.//td[normalize-space(.)='Credit Facility (CF) Details']][2]/following-sibling::table[1]"));

"Details of Settled Credit Facilities (Last 5 Years)" table:

driver.FindElement(By.Xpath("//td[normalize-space(.)='Details of Settled Credit Facilities (Last 5 Years)' and @class='tblHeader']/ancestor::table[1]"));

"Summary of Potential and Current Liabilities (Excluding Settlements)" table:

driver.FindElement(By.Xpath("//td[normalize-space(.)='Summary of Potential and Current Liabilities (Excluding Settlements)' and @class='tblHeader']/ancestor::table[2]//table[2]"));

获取除第一个 "Credit Facility (CF) Details" header 之外的所有行 table:

var rows = driver.FindElements(By.Xpath("//table[.//td[normalize-space(.)='Credit Facility (CF) Details']][1]/following-sibling::table[1]//tr[not(@type='table-header')]"));

foreach (IWebElement row in rows)
{
    var cfType = row.FindElement(By.Xpath(".//td[4]")).Text.Trim();
    var cfStat = row.FindElement(By.Xpath(".//td[5]")).Text.Trim();

    if ((cfType.Equals("LOAN") || cfType.Equals("LEAS") || cfType.Equals("CRCD") || cfType.Equals("OVDR")) && cfStat.Equals("ACTV") && cfStat.Equals("OWN"))
    {
        foreach (IWebElement colsDaysArrers in driver.FindElements(By.Xpath("//tr[./td[@class='textbrownNew' and @counter='true']]/td[not(@counter='true')]")))
        {
            var text = colsDaysArrers.Text;
            if (!(text.Equals("--") || text.Equals("OK") || text.Equals("")))
            {
                if (double.Parse(text) > 90)
                {
                    crib.FACILITYARREARSSTATUS = "Irrigular";
                    break;
                }
                else
                {
                    crib.FACILITYARREARSSTATUS = "Regular";
                }
            }
        }
    }
}

您可以使用下面的 Xpath - (//*[@id='bandstyle-Ver2'])[2]//tr

此处数字 2 是 table 数字,您可以根据需要使用 1 ,3,4。

(//*[@id='bandstyle-Ver2'])[2]//tr[1]/td[1]

上面的 xpath 会给你第二行第一行的 td table。

现在您可以编写一个逻辑使其通用。

如果您需要更多解释,请发表评论