Selenium WebDriver可以结合ExpectedCondition类来定义自己期望的条件

创建一个新的ExpectedCondition接口,必须实现apply方法

 

等待元素出现

 1 public void testWithImplicitWait(){
 2     System.setProperty("webdriver.chrome.driver", "chromedriver.exe");
 3     WebDriver driver = new ChromeDriver();
 4     driver.get("http://map.baidu.com");
 5 
 6         //点击展开当前城市
 7         WebElement curCity = driver.findElement(By.id("curCity"));
 8     curCity.click();
 9 
10         //设置等待时间10秒
11     WebDriverWait wait = new WebDriverWait(driver,10);
12     //创建一个新的ExpecctedCondition接口,就必须实现apply方法
13     WebElement message = wait.until(
14             new ExpectedCondition<WebElement>(){
15                 public WebElement apply(WebDriver d){
16                     return d.findElement(By.id("selCityHotCityId"));
17                 }
18             }
19             );
20 
21         driver.quit();
22     }    
示例代码

相关文章: