Selenium2设置隐式等待和显示等待


1. 设置显示等待

Java代码:


​​public​​                       ​​static​​                       ​​WebElement WaitForElement(WebDriver driver, String locator)​​          


​​{​​


​​WebElement targetElement = (​​ ​​new​​ ​​WebDriverWait(driver,​​ ​​10​​ ​​)).until(ExpectedConditions.presenceOfElementLocated(By.xpath(locator)));​​


​​return​​ ​​targetElement; ​​


​​}​​



  

 




1 WebDriver driver = new FirefoxDriver(); driver.get("http://somedomain/url_that_delays_loading"); 
2 WebElement myDynamicElement = (new WebDriverWait(driver, 10)) .until(ExpectedConditions.presenceOfElementLocated(By.id("myDynamicElement")));



 

 

2. 设置隐式等待

Java代码:

​​WebDriver driver = ​​           ​​new​​                       ​​FirefoxDriver();​​          


​​driver.manage().timeouts().implicitlyWait(​​ ​​10​​ ​​, TimeUnit.SECONDS);​​


​​driver.get(​​ ​​"http://i8xiaoshi.com"​​ ​​);​​


​​WebElement myDynamicElement = driver.findElement(By.id(​​ ​​"xxxx"​​ ​​));​​



1 WebDriver driver = new FirefoxDriver(); driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS); driver.get("http://somedomain/url_that_delays_loading"); 
2 WebElement myDynamicElement = driver.findElement(By.id("myDynamicElement"));