Explicit Wait 也就是显示等待,在使用的时候我们会设置一个timeout时间,如10秒,也就是在10秒内会间隔500毫秒尝试找一下元素,如果找到就返回true,10秒内找不到会返回timeout
例子:
WebDriver driver = new FirefoxDriver(); driver.get("http://somedomain/url_that_delays_loading"); WebElement myDynamicElement = (new WebDriverWait(driver, 10)) .until(ExpectedConditions.presenceOfElementLocated(By.id("myDynamicElement")));
也可以写成:
WebDriverWait wait = new WebDriverWait(driver, 10);
WebElement element = wait.until(ExpectedConditions.elementToBeClickable(By.id("someid")));
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Implicit Wait也就是隐示等待,相当于设置全局的等待,在定位元素时,对所有元素设置超时时间
例子:
WebDriver driver = new FirefoxDriver();
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
driver.get("http://somedomain/url_that_delays_loading");
WebElement myDynamicElement = driver.findElement(By.id("myDynamicElement"));
一般推荐使用显示等待