隐式等待相当于设置全局的等待,在定位元素时,对所有元素设置超时时间。
隐式等待使得WebDriver在查找一个Element或者Element数组时,每隔一段特定的时间就会轮询一次DOM,如果Element或数组没有马上被发现的话。
默认设置是0。一旦设置,这个隐式等待会在WebDriver对象实例的整个生命周期起作用。一劳永逸。
package com.test.elementwait; import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.concurrent.TimeUnit; import org.openqa.selenium.By;
import org.openqa.selenium.NoSuchElementException;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.WebDriverWait; public class ImplicitWait { public static void main(String[] args) {
WebDriver driver = new FirefoxDriver();
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
driver.get("http://www.baidu.com");
driver.manage().window().maximize(); try {
SimpleDateFormat format = new SimpleDateFormat("HH-mm-ss-SSS");
String time = format.format(Calendar.getInstance().getTime());
System.out.println("开始的时间: " + time); driver.findElement(By.id("kw22")).sendKeys("selenium"); } catch (NoSuchElementException e) {
System.out.println("没有找到元素");
e.printStackTrace();
} finally {
SimpleDateFormat format2 = new SimpleDateFormat("HH-mm-ss-SSS");
String time2 = format2.format(Calendar.getInstance().getTime());
System.out.println("结束的时间: " + time2);
driver.quit();
} } }
执行结果:
开始的时间: 23-12-26-775
没有找到元素
org.openqa.selenium.NoSuchElementException: Unable to locate element: {"method":"id","selector":"kw22"}
Command duration or timeout: 10.46 seconds
For documentation on this error, please visit: http://seleniumhq.org/exceptions/no_such_element.html
Build info: version: '2.53.0', revision: '35ae25b', time: '2016-03-15 16:57:40'
8 Driver info: org.openqa.selenium.firefox.FirefoxDriver
Capabilities [{applicationCacheEnabled=true, rotatable=false, handlesAlerts=true, databaseEnabled=true, version=45.2.0, platform=WINDOWS, nativeEvents=false, acceptSslCerts=true, webStorageEnabled=true, locationContextEnabled=true, browserName=firefox, takesScreenshot=true, javascriptEnabled=true, cssSelectorsEnabled=true}]
Session ID: dda0673c-da3d-4173-a904-d17148a3e26e
*** Element info: {Using=id, value=kw22}
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:408)
at org.openqa.selenium.remote.ErrorHandler.createThrowable(ErrorHandler.java:206)
at org.openqa.selenium.remote.ErrorHandler.throwIfResponseFailed(ErrorHandler.java:158)
at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:678)
at org.openqa.selenium.remote.RemoteWebDriver.findElement(RemoteWebDriver.java:363)
at org.openqa.selenium.remote.RemoteWebDriver.findElementById(RemoteWebDriver.java:413)
at org.openqa.selenium.By$ById.findElement(By.java:218)
at org.openqa.selenium.remote.RemoteWebDriver.findElement(RemoteWebDriver.java:355)
at com.test.elementwait.ImplicitWait.main(ImplicitWait.java:26)
结束的时间: 23-12-37-273