如何处理 table 行和列
How to handle table rows and columns
我正在使用 Java 和 Selenium 编写测试。在我的目标应用程序 DOM 中,有很多 table 我需要经过。我曾经使用过类似的东西:
WebElement mytable = driver.findElement(By.xpath(".//table/tbody"));
List<WebElement> rows_table = mytable.findElements(By.tagName("tr"));
int rows_count = rows_table.size();
for (int row=0; row<rows_count; row++){
List<WebElement> Columns_row = rows_table.get(row).findElements(By.tagName("td"));
int columns_count = Columns_row.size();
System.out.println("Number of cells In Row "+row+" are "+columns_count);
for (int column=0; column<columns_count; column++){
String celtext = Columns_row.get(column).getText();
System.out.println("Cell Value Of row number "+row+" and column number "+column+" Is "+celtext);
}
System.out.println("--------------------------------------------------");
}
但我一直在寻找可以更轻松地处理 table 的东西,所以我搜索并发现:
1) selenium.getTable
2) GetTable(ElementFinder finder, JavascriptLibrary js)
但我找不到适合他们的任何好的代码示例。
长话短说我想知道是否有比找到 .//tr
或 .//td
更好的方法来处理 table?
中的行和列
类似问题的答案在以下线程中回复:
HashMap<Integer, HashMap<String, String>> tableData = new HashMap<Integer, HashMap<String, String>>();
HashMap<String, String> tableCellDataMap = null;
WebElement table = driver.findElement(By.id("<table_id>"));
/**
* Call this method to get Table Cell Data by passing WebElement table
* @Params table
*
* @author Fayaz
**/
public static HashMap<String, String> getTableData(WebElement table) {
List<WebElements> tableColHeaders = table.findElements(By.tagName("th"));
List<WebElements> tableRows = table.findElements(By.tagName("tr"));
// Start rowIndex with '1' because in the zeroth index we get 'th' tags
for(int rowIndex = 1; rowIndex < tableRows.size(); rowIndex++) {
List<WebElement> tableCells = tableRows.get(rowIndex).findElements(By.tagName("td"));
for(int cellIndex = 0; cellIndex < tableCells.size(); cellIndex++) {
String tableCellData = tableCells.get(cellIndex).getText();
String tableColName = tableColHeaders.get(cellIndex).getText();
tableCellDataMap = new HashMap<String, String>();
tableCellDataMap.put(tableColName, tableCellData);
}
tableData.put(rowIndex, tableCellDataMap);
}
return tableCellDataMap;
}
实际上,它甚至可以进一步模块化,使用 getTableHeader()、getRowSize()、getColSize()、getData( int rowIndex), getData(int colIndex)
如果我们有多个表,那么也循环表
getTable 未在 webdriver 中实现。但是你可以试试这个。
WebDriver driver = new ChromeDriver();
Selenium selenium = new WebDriverBackedSelenium(driver, "your website url");
selenium .getTable("xpath");
selenium.getTable("table.1.2");
getTable(tableCellAddress) 参数:
tableCellAddress - a cell address, e.g. "foo.1.4"
Returns: the text from the specified cell
Gets the text from a cell of a table. The cellAddress syntax tableLocator.row.column, where row and column start at 0.
为方法添加抑制警告。
我正在使用 Java 和 Selenium 编写测试。在我的目标应用程序 DOM 中,有很多 table 我需要经过。我曾经使用过类似的东西:
WebElement mytable = driver.findElement(By.xpath(".//table/tbody"));
List<WebElement> rows_table = mytable.findElements(By.tagName("tr"));
int rows_count = rows_table.size();
for (int row=0; row<rows_count; row++){
List<WebElement> Columns_row = rows_table.get(row).findElements(By.tagName("td"));
int columns_count = Columns_row.size();
System.out.println("Number of cells In Row "+row+" are "+columns_count);
for (int column=0; column<columns_count; column++){
String celtext = Columns_row.get(column).getText();
System.out.println("Cell Value Of row number "+row+" and column number "+column+" Is "+celtext);
}
System.out.println("--------------------------------------------------");
}
但我一直在寻找可以更轻松地处理 table 的东西,所以我搜索并发现:
1) selenium.getTable
2) GetTable(ElementFinder finder, JavascriptLibrary js)
但我找不到适合他们的任何好的代码示例。
长话短说我想知道是否有比找到 .//tr
或 .//td
更好的方法来处理 table?
类似问题的答案在以下线程中回复:
HashMap<Integer, HashMap<String, String>> tableData = new HashMap<Integer, HashMap<String, String>>();
HashMap<String, String> tableCellDataMap = null;
WebElement table = driver.findElement(By.id("<table_id>"));
/**
* Call this method to get Table Cell Data by passing WebElement table
* @Params table
*
* @author Fayaz
**/
public static HashMap<String, String> getTableData(WebElement table) {
List<WebElements> tableColHeaders = table.findElements(By.tagName("th"));
List<WebElements> tableRows = table.findElements(By.tagName("tr"));
// Start rowIndex with '1' because in the zeroth index we get 'th' tags
for(int rowIndex = 1; rowIndex < tableRows.size(); rowIndex++) {
List<WebElement> tableCells = tableRows.get(rowIndex).findElements(By.tagName("td"));
for(int cellIndex = 0; cellIndex < tableCells.size(); cellIndex++) {
String tableCellData = tableCells.get(cellIndex).getText();
String tableColName = tableColHeaders.get(cellIndex).getText();
tableCellDataMap = new HashMap<String, String>();
tableCellDataMap.put(tableColName, tableCellData);
}
tableData.put(rowIndex, tableCellDataMap);
}
return tableCellDataMap;
}
实际上,它甚至可以进一步模块化,使用 getTableHeader()、getRowSize()、getColSize()、getData( int rowIndex), getData(int colIndex)
如果我们有多个表,那么也循环表
getTable 未在 webdriver 中实现。但是你可以试试这个。
WebDriver driver = new ChromeDriver();
Selenium selenium = new WebDriverBackedSelenium(driver, "your website url");
selenium .getTable("xpath");
selenium.getTable("table.1.2"); getTable(tableCellAddress) 参数:
tableCellAddress - a cell address, e.g. "foo.1.4"
Returns: the text from the specified cell
Gets the text from a cell of a table. The cellAddress syntax tableLocator.row.column, where row and column start at 0.
为方法添加抑制警告。