Java&Selenium處理頁面Table以及Table中隨機位置的資料
阿新 • • 發佈:2018-12-10
一、摘要
前一段時間公司小夥伴剛剛接觸自動化,遇到的一個問題,頁面新建立的資料儲存後,出現在table中的某個位置,並不一定是第一行還是第幾行,這種情況下如何去操控它
本篇博文將介紹處理這個問題的一種方式
二、測試程式碼
@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; } }