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;
}
任何人可以請告訴我如何修改這個以便能夠識別隱藏的現有元素?提前致謝!