【问题标题】:Getting data from table using Selenium使用 Selenium 从表中获取数据
【发布时间】:2017-02-27 18:29:38
【问题描述】:

我有一张费用表,我正试图解析以返回数据,但它在实际返回数据字符串之前返回了一些空白。

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

我通过 xpath 返回

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

硒代码:

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

问题

  1. ControlType.Label 是否适用于表格?在实际获取数据之前,我得到了一些空白元素。
  2. 如果我想将每个项目中的每个费率、描述和费用分开,以确保成本正确地加到总计中,我该怎么做?

【问题讨论】:

  • GetText() 是做什么的,GetNativeElements(By by, int num) 是什么?我希望您只想获得包含费用的列,而不是当您添加到实际时的整行,这看起来就像您现在正在做的那样。还涉及到ints的转换,这是一个相当大的问题。
  • 您是否真的想存储/使用费率和描述,还是获得足够好的回报?

标签: c# selenium selenium-webdriver


【解决方案1】:

我会做类似下面的事情。我创建了一个类Fee,其中包含费用的各个部分:代码、描述和金额。对于每个表行,您将提取这三个值并将它们存储在 Fee 类的实例中。该函数返回Fee 实例的集合。要获得费用总和,您可以调用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.,]", ""));
}

【讨论】:

    【解决方案2】:
    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]);
                    }
                }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-04-02
      • 1970-01-01
      • 2020-12-16
      • 2021-08-21
      • 1970-01-01
      • 2021-08-18
      • 1970-01-01
      • 2015-09-08
      相关资源
      最近更新 更多