Robotium Table控件的处理

it2022-05-06  30

HTML代码:

1 <html> 2 <head> 3 <title>Table</title> 4 </head> 5 <body> 6 <table border="1" id="myTable"> 7 <tr> 8 <th>列标题A(1 ,1)</th> 9 <th>列标题B(1 ,2)</th> 10 <th>列标题C(1 ,3)</th> 11 </tr> 12 <tr> 13 <td>2行, 1格</td> 14 <td>2行, 2格</td> 15 <td>2行, 3格</td> 16 </tr> 17 <tr> 18 <td>3行, 1格</td> 19 <td>3行, 2格</td> 20 <td>3行, 3格</td> 21 </tr> 22 </table> 23 </body> 24 </html>

JAVA代码:

1 package com.test; 2 3 import java.util.List; 4 import org.openqa.selenium.By; 5 import org.openqa.selenium.WebDriver; 6 import org.openqa.selenium.WebElement; 7 import org.openqa.selenium.chrome.ChromeDriver; 8 9 public class Test_table { 10 private WebDriver driver; 11 12 Test_table(WebDriver driver){ 13 this.driver = driver; 14 } 15 16 /** 17 * 获取指定行和列的文本 18 */ 19 public String getCellText(By by, int tableColunm, int tableRow) { 20 //得到table元素对象 21 WebElement table = driver.findElement(by); 22 //设置单元格位置(行、列)。 23 int row = tableRow; 24 int column = tableColunm; 25 //得到table表中所有行对象,并得到所要查询的行对象。 26 List<WebElement> rows = table.findElements(By.tagName("tr")); 27 WebElement theRow = rows.get(row); 28 //调用getCell方法得到对应的列对象,然后得到要查询的文本。 29 String text = getCell(theRow, column).getText(); 30 return text; 31 } 32 33 private WebElement getCell(WebElement Row,int cell){ 34 List<WebElement> cells; 35 WebElement target = null; 36 //列里面有"th"、"td"两种标签,所以分开处理。 37 if(Row.findElements(By.tagName("th")).size()>0){ 38 cells = Row.findElements(By.tagName("th")); 39 target = cells.get(cell); 40 } 41 if(Row.findElements(By.tagName("td")).size()>0){ 42 cells = Row.findElements(By.tagName("td")); 43 target = cells.get(cell); 44 } 45 return target; 46 } 47 48 public static void main(String[] args) { 49 String url = "file:///C:/Documents and Settings/fy/桌面/table.html"; 50 //打开chrome 51 WebDriver dr = new ChromeDriver(); 52 dr.get(url); 53 54 Test_table table = new Test_table(dr); 55 System.out.println(table.getCellText(By.id("myTable"), 0, 2)); 56 System.out.println(table.getCellText(By.id("myTable"), 2, 1)); 57 58 dr.quit(); 59 } 60 }

页面输出:

1 HeadingC(1 ,3) 2 3, 2

 

转载于:https://www.cnblogs.com/CharlesGrant/p/4076884.html


最新回复(0)