我是Selenium和Java的新手。我想要完成的是等待一个元素出现超时,如果该元素出现在超时运行之前,那么就继续运行。下面的代码将给出一个TimeoutException
并停止其余的代码运行。
WebElement myDynamicElement = (new WebDriverWait(driver, 10))
.until(ExpectedConditions.presenceOfElementLocated(By.id("my_element")));
请尝试这种方式:
public boolean isFind(WebElement my_element) {
try{
WebElement myDynamicElement = (new WebDriverWait(driver, 10))
.until(ExpectedConditions.presenceOfElementLocated(By.id("my_element")));
}
catch(TimeoutException exception) {
return false;
}
return true;
}
谢谢你的帮助,我花了一些时间来玩它。这是我提出的解决方案。希望它有助于下一个人。请告诉我,如果我搞砸了,请再次感谢。
try{
WebDriverWait wait = new WebDriverWait(driver, 5);
wait.until(ExpectedConditions.presenceOfElementLocated(By.xpath("//input[@id='signup_email']")));
}catch (org.openqa.selenium.TimeoutException e){
JOptionPane.showMessageDialog(null, "Login Complete!");
return;
}
你可以使用ExpectedConditions.visibilityOf
WebDriverWait wait = new WebDriverWait(d, 120);
wait.until(ExpectedConditions.visibilityOf(d.findElement(By.xpath("//input[@id='signup_email']"))));