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

前端 未结 6 1850
渐次进展
渐次进展 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: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 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 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++;
            } 
        }
    }
    

提交回复
热议问题