我有这个按钮:
url:'/sendmail'
使用<div class="container">
<div class="alert alert-danger">
<strong>Pozor!</strong> Je třeba aktualizovat formulář kvůli změnám hodnot v personálním formuláři. <a href="#" id="newVehicles_reCompPersonnelInput" class="btn btn-xs btn-danger pull-right reCompPersonnelInput">Přepočítat</a>
</div>
</div>
但按钮仅在某些情况下出现。我怎样才能避免NoSuchElementException?
答案 0 :(得分:2)
您可以使用findElements
,如果该元素不存在,它只会返回一个空列表,而不需要昂贵的try - catch
List<WebElement> prepocitat = driver.findElements(By.id("newVehicles_reCompPersonnelInput"));
if (prepocitat.size > 0) {
prepocitat.get(0).click();
}
答案 1 :(得分:-2)
try
{
WebElement prepocitat =
driver.findElement(By.id("newVehicles_reCompPersonnelInput"));
}
catch(NoSuchElementException)
{
do your action here
}
答案 2 :(得分:-2)
你可以做一个流利的等待
static Wait<WebDriver> wait = new FluentWait<WebDriver>(driver)
.withTimeout(elementWaitTime, SECONDS)
.pollingEvery(2,SECONDS)
.ignoring(NoSuchElementException.class);
public WebElement getAnyElement(By byLocator){
return wait.until(ExpectedConditions.visibilityOfElementLocated(locator));
}
使用方法或者你想要的方式
getAnyElement(By.id("someID")).click();
答案 3 :(得分:-2)
try
{
WebElement prepocitat = driver.findElement(By.id("newVehicles_reCompPersonnelInput"));
prepocitat.click();Thread.sleep(1000);
}
catch (org.openqa.selenium.NoSuchElementException e) { }