在Java /硒继续命令
问题描述:
这里是我的代码:在Java /硒继续命令
start:
for(int i = 0; i<sheet1.getRows()-1; i++, offerRow++){
//just some declarations here, I omitted them
driver.findElement(By.id("tab2")).click();
driver.findElement(By.id("new")).click();
driver.findElement(By.id("name")).clear();
driver.findElement(By.id("name")).sendKeys(offerName);
driver.findElement(By.id("save")).click();
System.out.println("Reached 0");
if(driver.findElement(By.id("infobar")).getText().equals("An offer already exists with that name.")){
offerRow = 1;
continue start;
}
System.out.println("Reached 1");
driver.findElement(By.id("productionend")).clear();
driver.findElement(By.id("productionend")).sendKeys(productionEnd);
System.out.println("Reached 2");
我的问题是,即使它继续启动的时候,如果声明是真实的,为什么没有去达到了1?我能做些什么才能继续?
答
JLS-14.16 The continue
Statement说(部分)
控制传递到循环语句的循环延续点。
你的情况,这i<sheet1.getRows()-1
和是什么continue
做;返回标签 ed循环延续(在这种情况下为start
)。我觉得你在一个内部循环想要一个else
,像
if (driver.findElement(By.id("infobar")).getText()
.equals("An offer already exists with that name.")){
offerRow = 1;
} else {
continue start;
}
但是,到continue
(如嵌套循环相对于外循环),你可以使用未标记continue
。像,
if (driver.findElement(By.id("infobar")).getText()
.equals("An offer already exists with that name.")){
offerRow = 1;
} else {
continue;
}
如果我在if语句上休息一下,它会再次启动for循环吗? –
不是。'break'会立即结束for循环(*因此* **不会达到'System.out.println(“达到1”)')。 –
啊。我真正想要发生的是,当if语句为true时,那么offerRow将被设置为1,然后再次启动for循环。否则,它会继续达到1.但它没有这样做。 –