標籤:lan try windows org style res version test base
隱式等待相當於設定全域的等待,在定位元素時,對所有元素設定逾時時間。
隱式等待使得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 secondsFor documentation on this error, please visit: http://seleniumhq.org/exceptions/no_such_element.htmlBuild info: version: ‘2.53.0‘, revision: ‘35ae25b‘, time: ‘2016-03-15 16:57:40‘ 8 Driver info: org.openqa.selenium.firefox.FirefoxDriverCapabilities [{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
selenium測試(Java)-- 隱式等待(十)