selenium测试(Java)-- 隐式等待(十)

隐式等待相当于设置全局的等待,在定位元素时,对所有元素设置超时时间。

隐式等待使得WebDriver在查找一个Element或者Element数组时,每隔一段特定的时间就会轮询一次DOM,如果Element或数组没有马上被发现的话。

默认设置是0。一旦设置,这个隐式等待会在WebDriver对象实例的整个生命周期起作用。一劳永逸。

 1 package com.test.elementwait;
 2 
 3 import java.text.SimpleDateFormat;
 4 import java.util.Calendar;
 5 import java.util.concurrent.TimeUnit;
 6 
 7 import org.openqa.selenium.By;
 8 import org.openqa.selenium.NoSuchElementException;
 9 import org.openqa.selenium.WebDriver;
10 import org.openqa.selenium.firefox.FirefoxDriver;
11 import org.openqa.selenium.support.ui.WebDriverWait;
12 
13 public class ImplicitWait {
14 
15     public static void main(String[] args) {
16         WebDriver driver = new FirefoxDriver();
17         driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
18         driver.get("http://www.baidu.com");
19         driver.manage().window().maximize();
20 
21         try {
22             SimpleDateFormat format = new SimpleDateFormat("HH-mm-ss-SSS");
23             String time = format.format(Calendar.getInstance().getTime());
24             System.out.println("开始的时间: " + time);
25 
26             driver.findElement(By.id("kw22")).sendKeys("selenium");
27 
28         } catch (NoSuchElementException e) {
29             System.out.println("没有找到元素");
30             e.printStackTrace();
31         } finally {
32             SimpleDateFormat format2 = new SimpleDateFormat("HH-mm-ss-SSS");
33             String time2 = format2.format(Calendar.getInstance().getTime());
34             System.out.println("结束的时间: " + time2);
35             driver.quit();
36         }
37 
38     }
39 
40 }

执行结果:

 1 开始的时间: 23-12-26-775
 2 没有找到元素
 3 org.openqa.selenium.NoSuchElementException: Unable to locate element: {"method":"id","selector":"kw22"}
 4 Command duration or timeout: 10.46 seconds
 5 For documentation on this error, please visit: http://seleniumhq.org/exceptions/no_such_element.html
 6 Build info: version: '2.53.0', revision: '35ae25b', time: '2016-03-15 16:57:40'
 7  8 Driver info: org.openqa.selenium.firefox.FirefoxDriver
 9 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}]
10 Session ID: dda0673c-da3d-4173-a904-d17148a3e26e
11 *** Element info: {Using=id, value=kw22}
12     at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
13     at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
14     at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
15     at java.lang.reflect.Constructor.newInstance(Constructor.java:408)
16     at org.openqa.selenium.remote.ErrorHandler.createThrowable(ErrorHandler.java:206)
17     at org.openqa.selenium.remote.ErrorHandler.throwIfResponseFailed(ErrorHandler.java:158)
18     at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:678)
19     at org.openqa.selenium.remote.RemoteWebDriver.findElement(RemoteWebDriver.java:363)
20     at org.openqa.selenium.remote.RemoteWebDriver.findElementById(RemoteWebDriver.java:413)
21     at org.openqa.selenium.By$ById.findElement(By.java:218)
22     at org.openqa.selenium.remote.RemoteWebDriver.findElement(RemoteWebDriver.java:355)
23     at com.test.elementwait.ImplicitWait.main(ImplicitWait.java:26)
24 结束的时间: 23-12-37-273

 

posted @ 2016-07-13 23:20  月色深潭  阅读(2639)  评论(0编辑  收藏  举报