zoukankan      html  css  js  c++  java
  • [Selenium]显式等待 Explicit wait & 隐式等待 Implicit wait

    显式等待 Explicit wait

    显示等待 , 就是明确的要等到某个元素出现或者某个元素满足某种条件,每隔一段时间检查一次,等不到,就一直等,如果在规定的时间内还没有找到,就跳出来
    检查间隔的时间和等待的最大时间可以自定义。

    等待某个元素的Y坐标满足某个条件,等待时间采用系统默认的时间

    Function<WebDriver, Boolean> waitFn = new Function<WebDriver, Boolean>() {
    @Override
    public Boolean apply(WebDriver driver) {
    	Point newPos = page.getWDGAttrDetail().getLocation();
    	return newPos.getY() != prePos.getY();
    	}
    };
    		
    SeleniumUtil.createWait(page.getDriver()).until(waitFn);
    

    等待某个元素存在,最多等10秒

    WebElement myDynamicElement = (new WebDriverWait(driver, 10)) .until(ExpectedConditions.presenceOfElementLocated(locator))
    

    等待某个元素可点击,最多等10秒

    new WebDriverWait(driver, 10). until(ExpectedConditions.elementToBeClickable(locator));
    

    等待某个元素可见,最多等10秒

    new WebDriverWait(driver, 10). until(ExpectedConditions.visibilityOf(locator));
    

    等待某个元素出现,每隔1s检查一次,最多等待120秒,等不到报错,提供错误信息

    //wait for processing icon present
    		Function<WebDriver, WebElement> waitFn = new Function<WebDriver, WebElement>() {
    			@Override
    			public WebElement apply(WebDriver driver) {
    				return el.findElement(By.cssSelector("div.rptstatus.rptcomplete"));
    			}
    		};
    		WebDriverWait wait = new WebDriverWait(driver, 120, 2);
    		wait.withMessage("A processing icon should appear in the Status column of the row '"+templateName+"'.");
    		wait.until(waitFn);
    

    隐式等待 Implicit wait

    隐式等待, 可以认为是一个全局的超时时间,它的影响是全局的,每次Driver找不到元素都会执行隐式等待

    driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS)
    
  • 相关阅读:
    flash使用lua
    如何写出兼容大部分浏览器的CSS 代码
    typeof 详解
    人月神话阅读笔记(三)
    人月神话阅读笔记(二)
    仓库物资管理
    动手动脑(四)
    人月神话阅读笔记(一)
    动手动脑(六 文件操作)及课后作业
    java异常处理
  • 原文地址:https://www.cnblogs.com/MasterMonkInTemple/p/3725992.html
Copyright © 2011-2022 走看看