我在Java中有以下for循环
List<String> accountList=new ArrayList<>();
int colNo=3;
for(int rowNo=1;rowNo<=tableRowNumber;rowNo++)
{
String accountName=getTableData(By.xPath(".//*[@id='accTable']/"), rowNo, colNo);
accountList.add(accountName);
}
方法getTableData(By,rowNo,colNo)用于检索表中的数据。当我运行代码时,如果没有数据是特定的单元格,则该方法将引发
org.openqa.selenium.NoSuchElementException:无法找到元素
例外。可以,但是循环的执行会在异常时停止。我如何忽略异常,以便即使发生异常也可以继续循环
使用一个try/catch
块,没有任何catch
for (....) {
try {
String accountName=getTableData(By.xPath(".//*[@id='accTable']/"), rowNo, colNo);
accountList.add(accountName);
} catch (NoSuchElementException ex) {
//Do nothing
}
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句