我有两种相同的方法。不幸的是,选项二有时有效(10 次中有 5 次)...选项一很少有效(10 次中有 2 次)...有没有更好的方法来编写此代码...
一:
public static void clickOnLinkinWebTable(WebElement webTable, String lookUpValue) throws MyException {
for (WebElement row : webTable.findElements(By.xpath("tr"))) {
for (WebElement col : row.findElements(By.xpath("td")))
if (col.getText().equals(lookUpValue)) {
col.findElement(By.partialLinkText(lookUpValue)).click();
return;
}
}
}
二:
public static void clickOnLinkinWebTable(String tableXpathRow, String lookUpValue) throws MyException {
int row_cnt = driver.findElements(By.xpath(tableXpathRow)).size();
for (int row_num = 1; row_num <= row_cnt; row_num++) {
int col_cnt = driver.findElements(By.xpath(tableXpathRow+"["+row_num+"]"+"/td")).size();
for (int col_num = 1; col_num <= col_cnt; col_num++) {
String cellValue = driver.findElement(By.xpath(tableXpathRow+"["+row_num+"]/td["+col_num+"]")).getText();
if(lookUpValue.equalsIgnoreCase(cellValue)){
WebElement elementLink = driver.findElement(By.xpath(tableXpathRow+"["+row_num+"]/td["+col_num+ "]/a"));
elementLink.click();
return;
}
}
}
}
有时/很少工作:我在测试套件中运行测试。此方法在测试套件中至少被调用 15 次(在同一页面中)。有时有效,但在调用此方法时有时无效。