我是编程的新手。这是我的情景。我正在测试一个网站。如果某些链接没有发现,它应该去阻止并转到下一步。如果它在k循环中失败,它应该做k ++;不是我++;或者j ++; 。如果它在j循环中失败,它应该增加j值而不是i值或k值。怎么做?
public static void main(String[] args) throws NoSuchElementException {
int i = 0, j = 0, k = 0;
String links[] = new String[10];
links[0] = "link1";
links[1] = "link2";
links[2] = "link3";
links[3] = "link4";
WebDriver driver = new FirefoxDriver();
while (i < 4) {
try {
driver.get(links[i]);
driver.findElement(By.xpath("//div[contains(@id,'image')]")).click();
while (j < 5) {
driver.findElement(By.xpath("//div[contains(@id,'header')]")).click();
while (k < 8) {
driver.findElement(By.xpath("//div[contains(@id,'title')]")).click();
k++;
}
j++;
}
i++;
} catch (NoSuchElementException e) {
System.out.println(e);
// How to increment the value of i or j or k
}
}
}
答案 0 :(得分:2)
在代码的顶部,有:
char currentLoop = 'i';
然后在每个while循环声明之后立即相应地设置值,如下所示:
while (i < 4) {
currentLoop = 'i';
...
while (j < 5) {
currentLoop = 'j';
...
while (k < 8) {
currentLoop = 'k';
...
}
}
}
然后在catch do:
if (currentLoop == 'i') {
i++;
}else if (currentLoop == 'j') {
j++;
}else if (currentLoop == 'k') {
k++;
}