1
我目前正在尝试使用硒进行一些用户界面测试,并且我遇到了这个不错的方法(不知道我从哪里得到它)..它假定照顾不存在要素和隐藏要素......selenium ElementNotVisibleException无法捕捉到隐藏的元素
问题就出在第二个陷阱:该方法保持返回“真”,即使没有显示元素/隐藏(visibility:hidden的)
public boolean elementExists(By locator, WebDriver driver) {
WebElement foo = null;
try {
foo = this.getElementByLocator(locator, 10, driver);
} catch (TimeoutException te) {
System.out
.println("Timeout - Dieses Element konnte nicht gefunden werden: "
+ locator.toString());
return false;
}
catch (ElementNotVisibleException env) {
System.out
.println("Dieses Element wurde gefunden, ist aber nicht sichtbar: "
+ locator.toString());
return false;
}
if (foo == null) {
return false;
} else {
return true;
}
}
public WebElement getElementByLocator(By locator, int timeout,
WebDriver driver) {
System.out.println("Rufe Methode getElementByLocator: "
+ locator.toString());
int interval = 5;
if (timeout <= 20)
interval = 3;
if (timeout <= 10)
interval = 2;
if (timeout <= 4)
interval = 1;
Wait<WebDriver> wait = new FluentWait<WebDriver>(driver)
.withTimeout(timeout, TimeUnit.SECONDS)
.pollingEvery(interval, TimeUnit.SECONDS)
.ignoring(NoSuchElementException.class,
StaleElementReferenceException.class);
WebElement we = wait.until(ExpectedConditions
.presenceOfElementLocated(locator));
return we;
}
任何人可以请告诉我如何修改这个以便能够识别隐藏的现有元素?提前致谢!