表類:
public Table {
private WebDriver driver;
private By baseLocator;
private By headCellLocator;
private By bodyRowLocator;
private By bodyCellLocator;
private String[] headColumns;
private List<WebElement> findRows;
public Tabele(WebDriver, driver, By baseLocator) {
this.driver = driver;
this.baseLocator = baseLocator;
}
public Table init(headCellLocator, bodyRowLocator, bodyCellLocator) {
this.headCellLocator = headCellLocator;
this.bodyRowLocator = bodyRowLocator;
this.bodyCellLocator = bodyCellLocator;
this.readHeadColumns();
return this;
}
private readHeadColumns() {
List<WebElement> headCells = this.driver
.findElement(this.baseLocator)
.findElements(this.headCellLocator);
for(WebElement headCell:headCells) {
ArrayUtils.add(this.headColumns, headCell.getText().trim())
}
}
public Table findRows() {
this.findRows = this.driver
.findElement(this.baseLocator)
.findElements(this.bodyRowLocator);
return this;
}
public Table findRows(String columnName, String columnValue) {
int columnIndex = ArrayUtils.indexOf(this.headColumns, columnName);
findRows();
List<WebElement> matchRows = new ArrayList<WebElement>();
for(WebElement row:findRows) {
String cellText = row.findElements(this.bodyCellLocator)
.get(columnIndex)
.getText()
.trim();
if(cellText.equals(columnValue)) {
matchRows.add(row);
}
}
this.findRows = matchRows;
return this;
}
public String[] readColumnValue(String columnName) {
int columnIndex = ArrayUtils.indexOf(this.headColumns, columnName);
String[] columnValue;
String cellText;
for(WebElement row: findRows) {
cellText = String cellText = row.findElements(this.bodyCellLocator)
.get(columnIndex)
.getText()
.trim();
ArrayUtils.add(columnValue, cellText);
}
return columnValue;
}
}
Table table = new Table(By.cssLocator('body .footable'));
table.init(By.cssLocator('thead th'), By.cssLocator('tbody > tr'), By.cssLocator('td'));
table.findRows().readColumnValue('PNR No');
// or
table.findRows('Origin', 'Dubai Intl Airport').readColumnValue('PNR No');
我的意思是,我需要根據td打印td的值它的價值。 –
發佈你的代碼你試過的是什麼 – zsbappa
創建一個以「行(tr)」元素爲輸入的類,然後將每個「td」映射到它根據標題創建的變量。 –