zoukankan      html  css  js  c++  java
  • selenium 延迟等待的三种方式

    1、最直接普通的方式:这个是设置固定的等待时间

       Thread.sleep(1000);  

    2、显示等待方式(Explicit Wait):就是明确的要等待的元素在规定的时间之内都没找到,那么就抛出Exception.代码示例如下

    public LoginPage LoginExpectingFailure(){
    WebDriverWait wait = new WebDriverWait(driver,10);

    wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("dologin")));

    这里,10是要等待的秒数.如果没有满足until()方法中的条件,就会始终在这里wait 10秒,依然找不到,就抛出异常 。

    还可以这样写:

     

        WebDriver chrome= new ChromeDriver() ;
        chrome.get( http://somedomain/url_that_delays_loading);
        WebElement e = (new WebDriverWait( chrome, 10)) .until(
            new ExpectedCondition< WebElement>(){
                @Override  //方法重写
                public WebElement ByPresent( WebDriver d) {
                    return d.findElement( By.id("id locator"));
                }
            }
        );   
    这样就通过回调函数,直接获得了这个WebElement.也就是页面元素 。

    3、隐式等待方式(implicitlyWait):设置脚本在查找元素时的最大等待时间;

    隐式等待方式(implicitlyWait):设置脚本在查找元素时的最大等待时间;
       driver.manage().timeouts().implicitlyWait(second, TimeUnit.SECONDS);    
    代码示例如下:
        public boolean isByElementDisplayed(By by, int time,WebDriver chrome) {
            boolean status = true;
            while(!isByPresent(chrome, by)){
                chrome.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS);
            }
            return status;      
        }
        
        public boolean isByPresent(WebDriver chrome, By by){
            boolean display = false;
            try{
                chrome.findElement(by).isDisplayed();
                return display= true;
            }catch(NoSuchElementException e){
                return display;
            }
        }  
    这里用while循环是如果明确需要查找的元素未找到,就继续循环等待;
  • 相关阅读:
    人工智能-实验一策略迭代和值迭代
    Lecture 3: Planning by Dynamic Programming
    Lecture 2: Markov Decision Processes
    Software Testing -- LAB02-Selenium上机实验
    数据结构-王道2017-第4章 树与二叉树-二叉树的遍历
    数据结构-王道2017-第4章 树与二叉树
    PHP-基础知识
    nginx的安装与配置
    在Linux上部署项目
    zk 命令
  • 原文地址:https://www.cnblogs.com/alisapan/p/6483329.html
Copyright © 2011-2022 走看看