How to get text from each cell of an HTML table?

前端 未结 6 1841
渐次进展
渐次进展 2020-12-13 03:15

In Selenium 2.0, I have no idea how to traverse through a HTML table in a webpage. In selenium2.0 javadoc, I found two classes \"TableFinder\" and \"TableCellFinder\", but

相关标签:
6条回答
  • 2020-12-13 03:25

    its

    selenium.getTable("tablename".rowNumber.colNumber)

    not

    selenium.getTable("tablename".colNumber.rowNumber)

    0 讨论(0)
  • 2020-12-13 03:27
    $content = '';
        for($rowth=0; $rowth<=100; $rowth++){
            $content .= $selenium->getTable("tblReports.{$rowth}.0") . "\n";
            //$content .= $selenium->getTable("tblReports.{$rowth}.1") . "\n";
            $content .= $selenium->getTable("tblReports.{$rowth}.2") . " ";
            $content .= $selenium->getTable("tblReports.{$rowth}.3") . " ";
            $content .= $selenium->getTable("tblReports.{$rowth}.4") . " ";
            $content .= $selenium->getTable("tblReports.{$rowth}.5") . " ";
            $content .= $selenium->getTable("tblReports.{$rowth}.6") . "\n";
    
        }
    
    0 讨论(0)
  • 2020-12-13 03:33

    Another C# example. I just made an extension method for it.

    public static string GetCellFromTable(this IWebElement table, int rowIndex, int columnIndex)
        {
            return table.FindElements(By.XPath("./tbody/tr"))[rowIndex].FindElements(By.XPath("./td"))[columnIndex].Text;
        }
    
    0 讨论(0)
  • 2020-12-13 03:34

    Here's a C# example I just cooked up, loosely based on the answer using CSS selectors, hopefully of use to others for seeing how to setup a ReadOnlyCollection of table rows and iterate over it in MS land at least. I'm looking through a collection of table rows to find a row with an OriginatorsRef (just a string) and a TD with an image that contains a title attribute with Overdue by in it:

        public ReadOnlyCollection<IWebElement> GetTableRows()
        {
            this.iwebElement = GetElement();
            return this.iwebElement.FindElements(By.CssSelector("tbody tr"));
        }
    

    And within my main code:

            ...
            ReadOnlyCollection<IWebElement> TableRows;
            TableRows = f.Grid_Fault.GetTableRows();
    
            foreach (IWebElement row in TableRows)
            {
                if (row.Text.Contains(CustomTestContext.Current.OriginatorsRef) &&
                  row.FindElements(By.CssSelector("td img[title*='Overdue by']")).Count > 0)
                    return true;
            }
    
    0 讨论(0)
  • 2020-12-13 03:35

    I have not used Selenium 2. Selenium 1.x has selenium.getTable("tablename".columnNumber.rowNumber) to reach the required cell. May be you can use webdriverbackedselenium and do this.

    And you can get the total rows and columns by using

    int numOfRows = selenium.getXpathCount("//table[@id='tableid']//tr")

    int numOfCols=selenium.getXpathCount("//table[@id='tableid']//tr//td")
    
    0 讨论(0)
  • 2020-12-13 03:45

    Thanks for the earlier reply.

    I figured out the solutions using selenium 2.0 classes.

    import java.util.List;
    
    import org.openqa.selenium.By;
    import org.openqa.selenium.WebDriver;
    import org.openqa.selenium.WebElement;
    import org.openqa.selenium.ie.InternetExplorerDriver;
    
    public class WebTableExample 
    {
        public static void main(String[] args) 
        {
            WebDriver driver = new InternetExplorerDriver();
            driver.get("http://localhost/test/test.html");      
    
            WebElement table_element = driver.findElement(By.id("testTable"));
            List<WebElement> tr_collection=table_element.findElements(By.xpath("id('testTable')/tbody/tr"));
    
            System.out.println("NUMBER OF ROWS IN THIS TABLE = "+tr_collection.size());
            int row_num,col_num;
            row_num=1;
            for(WebElement trElement : tr_collection)
            {
                List<WebElement> td_collection=trElement.findElements(By.xpath("td"));
                System.out.println("NUMBER OF COLUMNS="+td_collection.size());
                col_num=1;
                for(WebElement tdElement : td_collection)
                {
                    System.out.println("row # "+row_num+", col # "+col_num+ "text="+tdElement.getText());
                    col_num++;
                }
                row_num++;
            } 
        }
    }
    
    0 讨论(0)
提交回复
热议问题