简体   繁体   中英

Getting data from table using Selenium

I have a table of fees I am trying to parse through to return data, but it is returning a few blanks before it actually returning the string of data.

<table id="Fees">
            <thead>
                <tr>
                    <th>Rate Code</th>
                    <th>Description</th>
                    <th>Amount</th>
                </tr>
            </thead>
            <tbody>
                    <tr>
                        <td class="code">A1</td>
                        <td>Charge Type 1</td>
                        <td class="amount">$11.20</td>
                    </tr>
                    <tr>
                        <td class="code">C2</td>
                        <td>Charge Type 2</td>
                        <td class="amount">$36.00</td>
                    </tr>
                    <tr>
                        <td class="code">CMI</td>
                        <td>Cuba Medical Insurance</td>
                        <td class="amount">$25.00</td>
                    </tr>
            </tbody>
            <tfoot>
                <tr>
                    <td colspan="2">Total:</td>
                    <td class="amount">$145.16</td>
                </tr>
            </tfoot>
</table>

I return by xpath

private By lst_Fee
{
    get { return By.XPath("//*[@id=\"Fees\"]/tbody/tr"); }
}

Selenium code:

IList<IWebElement> fees = GetNativeElements(lst_Fee, 5);
List<string> actual = new List<string>();
foreach (IWebElement elem in fees)
{
    actual.Add(GetText(elem, ControlType.Label));
}

Questions

  1. Is ControlType.Label correct for a table? I am getting a few blank elems before actually getting to the data.
  2. If I wanted to separate each Rate, Description and Fee out in each item to make sure the cost adds up to Total correctly, how can I do that?

I would do something like the below. I created a class Fee that holds the parts of a fee: the code, description, and amount. For each table row , you would extract these three values and store them in an instance of the Fee class. The function returns a collection of Fee instances. To get the sum of the fees themselves, you would call the GetFees() method and then iterate through the Fee instances summing the amount into the final Total.

public class Fee
{
    private String code;
    private String desc;
    private BigDecimal amount;

    private Fee(String _code, String _desc, BigDecimal _amount)
    {
        this.code = _code;
        this.desc = _desc;
        this.amount = _amount;
    }
}

public List<Fee> GetFees()
{
    List<Fee> fees = new ArrayList<Fee>();
    List<WebElement> rows = driver.findElements(By.cssSelector("#Fees > tbody > tr"));
    for (WebElement row : rows)
    {
        List<WebElement> cells = row.findElements(By.cssSelector("td"));
        fees.add(new Fee(cells.get(0).getText(), cells.get(1).getText(), parse(cells.get(2).getText(), Locale.US)));
    }

    return fees;
}

// borrowed from http://stackoverflow.com/a/23991368/2386774
public BigDecimal parse(final String amount, final Locale locale) throws ParseException
{
    final NumberFormat format = NumberFormat.getNumberInstance(locale);
    if (format instanceof DecimalFormat)
    {
        ((DecimalFormat) format).setParseBigDecimal(true);
    }
    return (BigDecimal) format.parse(amount.replaceAll("[^\\d.,]", ""));
}
You can grab all the column headers and as well the row data by the below code:
Happy coding =
 //// Grab the table
            IWebElement grid;
            grid = _browserInstance.Driver.FindElement(By.Id("tblVoucherLookUp"));
            IWebElement headercolumns = grid.FindElement(By.Id("tblVoucherLookUp"));
          _browserInstance.Driver.Manage().Timeouts().ImplicitlyWait(TimeSpan.FromSeconds(75));
            _browserInstance.ScreenCapture("Voucher LookUp Grid");

                //// get the column headers
            char[] character = "\r\n".ToCharArray();
            string[] Split = headercolumns.Text.Split(character);

            for (int i = 0; i < Split.Length; i++)
            {
                if (Split[i] != "")
                {
                    _log.LogEntry("INFO", "Voucher data", true,
                    Split + " Text matches the expected:" + Split[i]);
                }
            }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM