简体   繁体   English

使用Selenium从表中获取数据

[英]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 我通过xpath返回

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

Selenium code: Selenium代码:

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? ControlType.Label对于表是否正确? 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? 如果我想将每个项目中的每个费率,描述和费用分开,以确保费用正确加到Total,我该怎么做?

I would do something like the below. 我会做类似下面的事情。 I created a class Fee that holds the parts of a fee: the code, description, and amount. 我创建了一个课程Fee ,用于支付部分费用:代码,描述和金额。 For each table row , you would extract these three values and store them in an instance of the Fee class. 对于每个表行,您将提取这三个值并将它们存储在Fee类的实例中。 The function returns a collection of Fee instances. 该函数返回Fee实例的集合。 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. 要获得费用本身的总和,您可以调用GetFees()方法,然后迭代Fee实例,将amount汇总到最终总计中。

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]);
                }
            }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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