1

I'm trying to access html using selenium c# web driver.

Part of my html as follows (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>

In my html there have multiple inner tables. full html code To access this html file, I used following Selenium code.

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)
        {

        }

    }
} 

In the HTML file, there have tables with id = bandstyle-Ver2 I need to access 2nd table's data. To achive that I used above selenium code. But when I accessing table like that code. this error is occured.

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

can anyone please tell me how to access those data.

Updated: according to @sers answer I used following code:

    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"))
        {

        }
    }

Then I also have a problem. I need to access these tables. this is that HTML part In this table I need to get the largest number of each table and if that number > 90 there have logic. to achieve that I used following code.

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;

                        }
                    }
                }
            }

        }

    }
}

Please tell me is this code is fine to do this? or have any otherway to do this?

2 Answers 2

1

First "Credit Facility (CF) Details" table:

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

Second "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]"));

Get all rows except header of first "Credit Facility (CF) Details" 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";
                }
            }
        }
    }
}
Sign up to request clarification or add additional context in comments.

11 Comments

Where should I need to apply this
Please check my full html file too. hope that will be helped to you to help to me
I'm using C# sir, I used IWebElement instead if WebElement can you please tell me what is the i ? and there is no get method.
There were several mistakes I did, check it now
I really appreciate your help sir, please check my updated question. I used above code to change your answer. and I also need to get a largest no from next tables as I mentioned in my updated question part. Is this ok sir? or please give me an answer
|
0

You can use below Xpath - (//*[@id='bandstyle-Ver2'])[2]//tr

here number 2 is table number you can have 1 ,3,4 depending upon your requirement.

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

above xpath will give you td of first row of your second table.

Now you can write a logic to make it generic .

Comment if you need more explanation

1 Comment

If you can please check my complete HTML and please give me a complete answer sir, because I'm really stacked on this :(

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.