selenium獲取html的表格單元格數據


獲取網頁的表格的某個單元格的值,思路:

1、獲取表格

2、獲取表格的所有行

3、根據某一行獲取該行的所有列

4、根據某一列獲得該行該列的單元格值

根據以上思路,可以知道,只需要行、列就可以得到單元格的值,所以方法的參數就是行值,列值

代碼如下:

package com.table;

import java.util.List;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;

/** 
* @ClassName: TestTable 
* @Description: TODO(獲取表格的某個單元格的值) 
* @author qiaojiafei 
* @date 2015年12月4日 上午10:32:44 
*  
*/
public class TestTable {
    WebDriver dr = null;
    public void init() {
        System.setProperty("webdriver.chrome.bin", "D:/BaiduYunDownload/selenium/chromedriver.exe");
        dr = new ChromeDriver();
        dr.get("file:///D:/testhtml/table.html");
    }
    
    public void tearDown() {
        dr.quit();
    }
    
    public List<WebElement> getRow() {
        WebElement e_table = dr.findElement(By.id("myTable"));
        List<WebElement> e_row = e_table.findElements(By.tagName("tr"));
        int i = e_row.size();
        //System.out.println(i);
        return e_row;
    }
    
    public WebElement getCell(List<WebElement> list, int rowN, int colN) {
        List<WebElement> e_col = list.get(rowN-1).findElements(By.tagName("td"));
        return e_col.get(colN-1);
    }
    
    public String getTalbeValue(int rowN, int colN) {
        String s = getCell(getRow(), rowN, colN).getText();
        return s;
    }
    public static void main(String args[]) {
        TestTable tt = new TestTable();
        tt.init();
        System.out.println(tt.getTalbeValue(2, 1));
    }

}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM