selenium 如何處理table
阿新 • • 發佈:2019-01-09
以前在selenium RC 裡面有一個getTable方法,是得到一個單元格中的文字。其詳細描述如下:
Java程式碼- /** Gets the text from a cell of a table. The cellAddress syntax <span style="white-space: normal; background-color: #ffffff;">tableLocator.row.column</span>
- , where row and column start at 0.
-
@param tableCellAddress a cell address, e.g. <span style="white-space: normal; background-color: #ffffff;">"foo.1.4"</span>
- @return the text from the specified cell
- */
- String getTable(String tableCellAddress);
就是傳入一個引數,這個引數的格式必須是tableLocator.row.column,如"foo.1.4",foo用於得到table物件,1.4代表在table裡第1行第4列。行、列從0開始。
在selenium webdriver裡,沒有這樣的方法,也就是說沒有專門操作table的類。但我們可以自己封閉一個,這並不難。以上面的getTable方法為例,我們自己也可以建立這樣功能的一個方法。
Java程式碼- public String getCellText(By by,String tableCellAddress)
我叫它getCellText,它有兩個引數,第一個是By物件用於得到table物件, tableCellAddress 如"1.4",代表在table裡第1行第4列。行、列從0開始。
以下面html程式碼為例:
Html程式碼- <html>
- <head>
- <title>Table</title>
- </head>
-
<body>
- <table border="1" id="myTable">
- <tr>
- <th>Heading(row 0 ,cell 0)</th>
- <th>Another Heading(row 0 ,cell 1)</th>
- <th>Another Heading(row 0 ,cell 2)</th>
- </tr>
- <tr>
- <td>row 1, cell 0</td>
- <td>row 1, cell 1</td>
- <td>row 1, cell 2</td>
- </tr>
- <tr>
- <td>row 2, cell 0</td>
- <td>row 2, cell 1</td>
- <td>row 2, cell 2</td>
- </tr>
- </table>
- </body>
- </html>
示例程式碼如下:
Java程式碼- import java.util.List;
- import org.openqa.selenium.By;
- import org.openqa.selenium.NoSuchElementException;
- import org.openqa.selenium.WebDriver;
- import org.openqa.selenium.WebElement;
- import org.openqa.selenium.firefox.FirefoxDriver;
- public class Table {
- /**
- * @author gongjf
- */
- private WebDriver driver;
- Table(WebDriver driver){
- this.driver = driver;
- }
- /** 從一個table的單元格中得到文字值. 引數tableCellAddress的格式為
- row.column, 行列從0開始.
- @param by 用於得到table物件
- @param tableCellAddress 一個單元格地址, 如. "1.4"
- @return 從一個table的單元格中得到文字值
- */
- public String getCellText(By by,String tableCellAddress) {
- //得到table元素物件
- WebElement table = driver.findElement(by);
- //對所要查詢的單元格位置字串進行分解,得到其對應行、列。
- int index = tableCellAddress.trim().indexOf('.');
- int row = Integer.parseInt(tableCellAddress.substring(0, index));
- int cell = Integer.parseInt(tableCellAddress.substring(index+1));
- //得到table表中所有行物件,並得到所要查詢的行物件。
- List<WebElement> rows = table.findElements(By.tagName("tr"));
- WebElement theRow = rows.get(row);
- //呼叫getCell方法得到對應的列物件,然後得到要查詢的文字。
- String text = getCell(theRow, cell).getText();
- return text;
- }
- private WebElement getCell(WebElement Row,int cell){
- List<WebElement> cells;
- WebElement target = null;
- //列裡面有"<th>"、"<td>"兩種標籤,所以分開處理。
- if(Row.findElements(By.tagName("th")).size()>0){
- cells = Row.findElements(By.tagName("th"));
- target = cells.get(cell);
- }
- if(Row.findElements(By.tagName("td")).size()>0){
- cells = Row.findElements(By.tagName("td"));
- target = cells.get(cell);
- }
- return target;
- }
- public static void main(String[] args) {
- WebDriver driver;
- System.setProperty("webdriver.firefox.bin","D:\\Program Files\\Mozilla Firefox\\firefox.exe");
- driver = new FirefoxDriver();
- driver.get("file:///C:/Documents and Settings/Gongjf/桌面/selenium_test/table.html");
- Table table = new Table(driver);
- By by = By.id("myTable");
- String address = "0.2";
- System.out.println(table.getCellText(by, address));
- }
- }
執行程式碼將輸出
Java程式碼- Another Heading(row 0 ,cell 2)