获取网页的表格的某个单元格的值,直接上代码如下:
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));
}
}