我的函数遍历该表两次以查找同一行上存在的两个输入值。但是,当rowNo2和rowNo相等时,它并没有脱离循环。我做错了什么?谢谢!
WebElement table = oWebDriver.getWebElement(tableObject);
List<WebElement> rows = table.findElements(By.tagName("tr"));
List<WebElement> cols = table.findElements(By.tagName("td"));
int colSize = (cols.size()) / (rows.size());
int rowNo = 0;
int rowNo2 = 0;
int colNo = 0;
for (int i = 0; i < cols.size(); i++) {
System.out.println("row No2 = " + rowNo2);
System.out.println("row No1 = " + rowNo);
WebElement cellData = cols.get(i);
if (cellData.getText().contains(rowData)||cellData.getText().contains(rowData.toUpperCase())) {
int l = i / colSize;
rowNo = l + 1;
for (int k = 0; k < cols.size(); k++) {
WebElement cellData2 = cols.get(k);
if (cellData2.getText().contains(rowData2)||cellData2.getText().contains(rowData2.toUpperCase())) {
int n = k / colSize;
rowNo2 = n + 1;
if (rowNo2 == rowNo) {
break;
}
}
}
}
}
答案 0 :(得分:1)
您只是停止内部循环,而不是外部循环,要停止外部循环执行,还需要再添加一个条件。
有很多方法可以打破外循环,但这是简单的方法,在内循环i = cols.size();
条件之后添加if
行:
if (rowNo2 == rowNo) {
i = cols.size();
break;
}
一旦rowNo2 == rowNo
得到满足,这也将停止外循环执行。
答案 1 :(得分:0)
Java中有一个标记为中断的概念。也许您可以尝试这样的事情-
WebElement table = oWebDriver.getWebElement(tableObject);
List<WebElement> rows = table.findElements(By.tagName("tr"));
List<WebElement> cols = table.findElements(By.tagName("td"));
int colSize = (cols.size()) / (rows.size());
int rowNo = 0;
int rowNo2 = 0;
int colNo = 0;
here:for (int i = 0; i < cols.size(); i++) {
System.out.println("row No2 = " + rowNo2);
System.out.println("row No1 = " + rowNo);
WebElement cellData = cols.get(i);
if (cellData.getText().contains(rowData)||cellData.getText().contains(rowData.toUpperCase())) {
int l = i / colSize;
rowNo = l + 1;
for (int k = 0; k < cols.size(); k++) {
WebElement cellData2 = cols.get(k);
if (cellData2.getText().contains(rowData2)||cellData2.getText().contains(rowData2.toUpperCase())) {
int n = k / colSize;
rowNo2 = n + 1;
if (rowNo2 == rowNo) {
break here;
}
}
}
}
}