測試代碼
@Test
public void test_Table() throws Exception {//獲取表單,xpath是表單的定位
WebElement tableElement=driver.findElement(By.xpath("//*[@id='app']/section/section/main/section/div[1]/div[3]/table"));
//將表單的所有tr放進列表,每個tr是表單的一行,逐行遍歷
List<WebElement> rows=tableElement.findElements(By.tagName("tr"));
for (int i = 0; i < rows.size(); i++) {
//將表單的td放進list裏,每個td是表單的一列,逐列遍歷
List<WebElement> cols=rows.get(i).findElements(By.tagName("td"));
for (int j = 0; j < cols.size();) {
String tdText = cols.get(j).getText();
sleep(1000);
System.out.println(tdText +"\t");
//判斷哪行哪列的內容包含字段"mysql01", 如果包含則進行操作
if(tdText.contains("mysql01")){
System.out.println(i+1);
System.out.println(j+1);
int row = i + 1;
//點擊mysql01所在行的下拉按鈕
WebElement dropdown = driver.findElement(By.xpath("//*[@id='app']/section/section/main/section/div[1]/div[3]/table/tbody/tr["+row+"]/td[6]/div/div/span"));
dropdown.click();
}break;
}
}
}
實際上如果頁面存在檢索功能,完全可以寫幾步檢索操作,讓頁面只有一條你要的數據,那麼它的位置就是固定了,然後再進行操控
處理Table方法
package util;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import java.util.List;
import java.util.NoSuchElementException;
import static util.WaitElementUtil.sleep;
/*
* some method of controlling table
*
* @author: davieyang
* @create: 2018-08-05 14:04
*/
public class TableUtil {
//聲明一個WebElement對象,用於存儲頁面的表格元素對象
private WebElement _table;
//爲構造函數傳入頁面表格元素對象參數,調用TableUtil類的settable方法,將頁面表格元素賦值給TableUtil類的_table成員變量
public TableUtil (WebElement table){
setTable(table);
}
//獲取頁面表格對象的方法
public WebElement getTable(){
return _table;
}
//將頁面表格元素賦值給TableUtil類中_table成員變量的方法
public void setTable(WebElement _table){
this._table = _table;
}
//獲取表格元素的行數,查找表格元素有幾個tr元素,有幾個tr元素,就可以知道表格有幾行,tr數量和表格行數相一致
public int getRowCount(){
List<WebElement> tableRows = _table.findElements(By.tagName("tr"));
return tableRows.size();
}
//獲取表格元素的列數,使用get(0)從容器中取出表格第一行的元素,查找有幾個“td”,td數量和列數一致
public int getColumnCount(){
List<WebElement> tableRows = _table.findElements(By.tagName("tr"));
return tableRows.get(0).findElements(By.tagName("td")).size();
}
//獲取表格中某行某列的單元格對象
public WebElement getCell(int rowNo, int colNo)throws NoSuchElementException{
try{
List<WebElement> tableRows = _table.findElements(By.tagName("tr"));
System.out.println("行總數:" + tableRows.size());
System.out.println("行號:" + rowNo);
WebElement currentRow = tableRows.get(rowNo - 1);
List<WebElement> tableCols = currentRow.findElements(By.tagName("td"));
System.out.println("列總數:" + tableCols.size());
WebElement cell = tableCols.get(colNo-1);
System.out.println("列號:" + colNo);
return cell;
}catch (NoSuchElementException e){
throw new NoSuchElementException("沒有找到相關元素");
}
}
/**
* 獲得表格中某行某列的單元格中的某個頁面元素對象,by參數用於定位某個表格中的頁面元素,例如by.xpath("input[@type='text']")可以定義到表格中的輸入框
*/
public WebElement getWebElementInCell(int rowNo, int colNo, By by)throws NoSuchElementException{
try{
List<WebElement> tableRows = _table.findElements(By.tagName("tr"));
//找到表格中的某一行,行號從0開始,例如第三行,則需要進行3-1來獲取即“2”
WebElement currentRow = tableRows.get(rowNo-1);
List<WebElement> tableCols = currentRow.findElements(By.tagName("td"));
//找到表格中的某一列,因爲也是從0開始,所以要找到第三列,則需要進行3-1來獲取即“2”
WebElement cell = tableCols.get(colNo-1);
return cell.findElement(by);
}catch (NoSuchElementException e){
throw new NoSuchElementException("沒有找到相關元素");
}
}
/**
*
* @param driver 瀏覽器驅動
* @param row 行號
* @param column 列號
* @return 函數接受瀏覽器驅動,表格行數和列數,注意表頭行,返回某個cell的值
*/
public static String tableCell(WebDriver driver, int row, int column) {
String text = null;
//avoid get the head line of the table
row=row+1;
String xpath="//*[@id='table138']/tbody/tr["+row+"]/td["+column+"]";
WebElement table=driver.findElement(By.xpath(xpath));
text=table.getText();
return text;
}
}