Selenium Webdriver - 获取表数据

时间:2012-05-17 05:06:54

标签: uitableview selenium webdriver html-table

我想从UI中的表中获取数据。我知道使用“tr”和“td”循环遍历行和列。但是我拥有的那张桌子是这样的:

<table>
 <tbody>
  <tr><td>data</td><th>data</th><td>data</td><td>data</td></tr>
  <tr><td>data</td><th>data</th><td>data</td><td>data</td></tr>
  <tr><td>data</td><th>data</th><td>data</td><td>data</td></tr>
 </tbody>
</table>

如何使我的代码通用,以便可以处理中间“TH”的出现。 目前,我正在使用此代码:

// Grab the table
WebElement table = driver.findElement(By.id(searchResultsGrid));

// Now get all the TR elements from the table
List<WebElement> allRows = table.findElements(By.tagName("tr"));
// And iterate over them, getting the cells
for (WebElement row : allRows) {
 List<WebElement> cells = row.findElements(By.tagName("td"));
 for (WebElement cell : cells) {
 // And so on
 }
}

8 个答案:

答案 0 :(得分:15)

您可以查找tr元素的所有子元素,而无需区分td和th。而不是

List<WebElement> cells = row.findElements(By.tagName("td"));

我会用

List<WebElement> cells = row.findElements(By.xpath("./*"));

答案 1 :(得分:9)

Mayby对于这个问题的主人来说已经太晚了,但对其他人有帮助。

List<WebElement> cells = row.findElements(By.xpath(".//*[local-name(.)='th' or local-name(.)='td']"));

答案 2 :(得分:3)

// Grab the table
WebElement table = driver.findElement(By.id("searchResultsGrid"));

// Now get all the TR elements from the table
List<WebElement> allRows = table.findElements(By.tagName("tr"));
// And iterate over them, getting the cells
for (WebElement row : allRows) {
    List<WebElement> cells = row.findElements(By.tagName("td"));
    for (WebElement cell : cells) {
        System.out.println("content >>   " + cell.getText());
    }
}

使用cell.getText()只会工作

答案 3 :(得分:2)

您不需要遍历元素。相反,请使用ByChained定位器。

如果您的表格如下:

<table>
  <tbody>
    <tr><th>Col1</th><th>Col2</th><th>Col3</th></tr>
    <tr><td>data</td><td>data</td><td>data</td></tr>
    <tr><td>data</td><td>data</td><td>data</td></tr>
    <tr><td>data</td><td>data</td><td>data</td></tr>
  </tbody>
</table>

这样的定位:

By tableBodyLocator = By.xpath(".//table/tbody");
By headerRowLocator = By.xpath(".//tr[position()=1]");
By dataRowsLocator = By.xpath(".//tr[not(position()=1)]");

By headerRowLocator = new ByChained(tableBodyLocator, headerRowLocator);
List<WebElement> weHeaders = driver.findElement(headerRowLocator)
       .findElements(By.xpath(".//th");
List<WebElement> allRowData = driver.findElements(tableBodyLocator, dataRowsLocator);

WebElement row1Data = allRowData.get(0);
WebElement row2Data = allRowData.get(1);

etc.

答案 4 :(得分:0)

是的,它适用于c#与selenium ...

IList<IWebElement> cells = row.findElements(By.xpath(".//*[local-name(.)='th' or local-name(.)='td']"));

答案 5 :(得分:0)

IWebElement table = driver.FindElement(By.Id("id"));
List<IWebElement> allRows = new List<IWebElement> (table.FindElements(By.TagName("tr")));

foreach (var Row in allRows)
{
    List<IWebElement> cells = new List<IWebElement>( Row.FindElements(By.TagName("td")));
    foreach (var cel in cells)
    {
        string test = cel.Text;
    }
}

答案 6 :(得分:0)

下面的代码不仅可以获取表格的行和列,而且还可以获得它们在浏览器中的顺序,如果您在TD列中有嵌套结构,这将非常方便情况下。

 public DataTable StoreHtmlTableToDataTable(IWebElement tblObj,bool isFirstRowHeader = true)
        {
            DataTable dataTbl = new DataTable();
            int rowIndex = 0;

            try
            {               
                //_tblDataCollection = new List<TableDataCollection>();

                var tblRows = ((IJavaScriptExecutor)DriverContext.Driver).ExecuteScript("return arguments[0].rows; ", tblObj);

                if (tblRows != null)
                {
                    //Iterate through each row of the table
                    foreach (IWebElement tr in (IEnumerable)tblRows)
                    {                        
                        int colIndx = 0;
                        DataRow dtRow =  dataTbl.NewRow();
                        // Iterate through each cell of the table row
                        var tblCols = ((IJavaScriptExecutor)DriverContext.Driver).ExecuteScript("return arguments[0].cells; ", tr);
                        foreach (IWebElement td in (IEnumerable)tblCols)
                        {
                            //add the header row of the table as  the datatable column hader row
                            if (rowIndex == 0)
                            {
                                dataTbl.Columns.Add("Col" + colIndx.ToString(), typeof(string));
                            }

                            dtRow["Col"+colIndx.ToString()] = td.Text;

                            //loop through any child or nested table structures if you want using the same approach for example links,radio buttons etc inside the cell

                            //Write Table to List : This part is not done yet                           
                            colIndx++;
                        }
                        dataTbl.Rows.Add(dtRow);
                        rowIndex++;
                    }

                }


            }
            catch (Exception)
            {
                throw;
            }

            //if first row is the header row then assign it as a header of the datatable
            if (isFirstRowHeader)
            {
                dataTbl = this.AssignDataTableHeader(dataTbl);
            }

            return dataTbl;
        }

答案 7 :(得分:0)

TableDriver (https://github.com/jkindwall/TableDriver.Java) 支持带有 td 和 th 标签的单元格。将 TableDriver 与您的示例表一起使用将如下所示:

Table table = Table.createWithNoHeaders(driver.findElement(By.id(searchResultsGrid), 0);
List<TableRow> allRows = table.getRows();
for (TableRow row : allRows) {
    List<TableCell> cells = row.getCells();
    for (TableCell cell : cells) {
        // Access the cell's WebElement like this: cell.getElement()
    }
}