我正在开发一个Java Selenium-WebDriver。我添加了
driver.manage().timeouts().implicitlyWait(2, TimeUnit.SECONDS);
和
WebElement textbox = driver.findElement(By.id("textbox"));
因为我的应用程序需要几秒钟来加载用户界面。所以我设置了2秒隐含等待。但是我让找不到元素textbox
然后添加Thread.sleep(2000);
现在它工作得很好。哪种方法更好?
发布于 2012-10-12 21:02:26
有两种类型的等待:显式等待和隐式等待。显式等待的概念是
WebDriverWait.until(condition-that-finds-the-element);
隐式等待的概念是
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
你可以在details here中得到不同的结果。
在这种情况下,我更喜欢使用显式等待(特别是fluentWait
):
public WebElement fluentWait(final By locator) {
Wait<WebDriver> wait = new FluentWait<WebDriver>(driver)
.withTimeout(30, TimeUnit.SECONDS)
.pollingEvery(5, TimeUnit.SECONDS)
.ignoring(NoSuchElementException.class);
WebElement foo = wait.until(new Function<WebDriver, WebElement>() {
public WebElement apply(WebDriver driver) {
return driver.findElement(locator);
}
});
return foo;
};
fluentWait
函数返回找到的web元素。来自fluentWait
上的文档:可以动态配置超时和轮询间隔的等待接口的实现。每个FluentWait实例定义了等待条件的最长时间,以及检查条件的频率。此外,用户可以将等待配置为在等待时忽略特定类型的异常,例如在搜索页面上的元素时的NoSuchElementExceptions。可以从here获取的详细信息
在您的案例中,fluentWait
的用法如下:
WebElement textbox = fluentWait(By.id("textbox"));
这种方法更好,因为您不知道确切的等待时间,并且在轮询间隔中,您可以设置任意的timevalue,元素的存在将通过该timevalue进行验证。致以问候。
发布于 2014-11-08 08:39:06
这个帖子有点老了,但我想把我目前正在做的事情(正在进行的工作)贴出来。
虽然我仍然遇到系统负载很重的情况,当我单击提交按钮(例如,login.jsp)时,所有三个条件(见下文)都返回true
,但下一个页面(例如,home.jsp)尚未开始加载。
这是一个接受ExpectedConditions列表的泛型等待方法。
public boolean waitForPageLoad(int waitTimeInSec, ExpectedCondition<Boolean>... conditions) {
boolean isLoaded = false;
Wait<WebDriver> wait = new FluentWait<>(driver)
.withTimeout(waitTimeInSec, TimeUnit.SECONDS)
.ignoring(StaleElementReferenceException.class)
.pollingEvery(2, TimeUnit.SECONDS);
for (ExpectedCondition<Boolean> condition : conditions) {
isLoaded = wait.until(condition);
if (isLoaded == false) {
//Stop checking on first condition returning false.
break;
}
}
return isLoaded;
}
我已经定义了各种可重用的ExpectedConditions (下面有三个)。在本例中,三个预期条件包括document.readyState = 'complete‘、不存在"wait_dialog“和不存在”微调器“(指示正在请求异步数据的元素)。
一般情况下,只有第一个可以应用于所有网页。
/**
* Returns 'true' if the value of the 'window.document.readyState' via
* JavaScript is 'complete'
*/
public static final ExpectedCondition<Boolean> EXPECT_DOC_READY_STATE = new ExpectedCondition<Boolean>() {
@Override
public Boolean apply(WebDriver driver) {
String script = "if (typeof window != 'undefined' && window.document) { return window.document.readyState; } else { return 'notready'; }";
Boolean result;
try {
result = ((JavascriptExecutor) driver).executeScript(script).equals("complete");
} catch (Exception ex) {
result = Boolean.FALSE;
}
return result;
}
};
/**
* Returns 'true' if there is no 'wait_dialog' element present on the page.
*/
public static final ExpectedCondition<Boolean> EXPECT_NOT_WAITING = new ExpectedCondition<Boolean>() {
@Override
public Boolean apply(WebDriver driver) {
Boolean loaded = true;
try {
WebElement wait = driver.findElement(By.id("F"));
if (wait.isDisplayed()) {
loaded = false;
}
} catch (StaleElementReferenceException serex) {
loaded = false;
} catch (NoSuchElementException nseex) {
loaded = true;
} catch (Exception ex) {
loaded = false;
System.out.println("EXPECTED_NOT_WAITING: UNEXPECTED EXCEPTION: " + ex.getMessage());
}
return loaded;
}
};
/**
* Returns true if there are no elements with the 'spinner' class name.
*/
public static final ExpectedCondition<Boolean> EXPECT_NO_SPINNERS = new ExpectedCondition<Boolean>() {
@Override
public Boolean apply(WebDriver driver) {
Boolean loaded = true;
try {
List<WebElement> spinners = driver.findElements(By.className("spinner"));
for (WebElement spinner : spinners) {
if (spinner.isDisplayed()) {
loaded = false;
break;
}
}
}catch (Exception ex) {
loaded = false;
}
return loaded;
}
};
根据页面的不同,我可能会使用其中的一个或全部:
waitForPageLoad(timeoutInSec,
EXPECT_DOC_READY_STATE,
EXPECT_NOT_WAITING,
EXPECT_NO_SPINNERS
);
在下面的类中也有预定义的ExpectedConditions:org.openqa.selenium.support.ui.ExpectedConditions
发布于 2013-09-18 03:46:49
如果使用webdriverJs (node.js),
driver.findElement(webdriver.By.name('btnCalculate')).click().then(function() {
driver.sleep(5000);
});
上面的代码让浏览器在点击按钮后等待5秒钟。
https://stackoverflow.com/questions/12858972
复制相似问题