• (java)selenium webdriver学习---三种等待时间方法:显式等待,隐式等待,强制等待


    selenium webdriver学习---三种等待时间方法:显式等待,隐式等待,强制等待

    本例包括窗口最大化,刷新,切换到指定窗口,后退,前进,获取当前窗口url等操作;

    import java.util.Set;
    import java.util.concurrent.TimeUnit;
    
    import org.jsoup.Jsoup;
    import org.jsoup.nodes.Document;
    import org.omg.CORBA.PUBLIC_MEMBER;
    import org.openqa.selenium.By;
    import org.openqa.selenium.WebDriver;
    import org.openqa.selenium.WebElement;
    import org.openqa.selenium.chrome.ChromeDriver;
    import org.openqa.selenium.phantomjs.PhantomJSDriver;
    import org.openqa.selenium.support.ui.ExpectedCondition;
    import org.openqa.selenium.support.ui.ExpectedConditions;
    import org.openqa.selenium.support.ui.Wait;
    import org.openqa.selenium.support.ui.WebDriverWait;
    
    /**
     * 方法3,隐式等待,隐式等待相当于设置全局的等待,在定位元素时,对所有元素设置超时时间。
     * 隐式等待使得WebDriver在查找一个Element或者Element数组时,每隔一段特定的时间就会轮询一次DOM,
     * 如果Element或数组没有马上被发现的话。默认设置是0。一旦设置,这个隐式等待会在WebDriver对象实例的整个生命周期起作用。一劳永逸。
     * ********
     * 方法1,强制等待,固定休眠时间设置,Java的Thread类里提供了休眠方法sleep,导入包后就能使用,
     * 执行到此时不管什么就固定的等待5秒之后再接着执行后面的操作
     * ********
     * 方法2,显式等待,在0-10s中去定位id为“su”的元素,WebDriverWait默认每500ms就调用一次ExpectedCondition
     * 直到定位成功或者时间截止,ExpectedCondition的返回值要么为true要么为不为空的对象,
     * 在规定时间内若没有定位成功元素,则until()会抛出org.openqa.selenium.TimeoutException 。*/
    
    public class YsfTest_20180726{
        private static final int ExpectedCondition = 0;
        private static final int Boolean = 0;
    
        public static void main(String[] args) throws InterruptedException{
            WebElement search = null;
            System.setProperty("webdriver.chrome.driver","C:/Program Files (x86)/Google/Chrome/Application/chromedriver.exe");
            WebDriver driver = new ChromeDriver();
            //方法3,隐式等待,implicitlyWait()方法比sleep()方法智能,sleep()方法只能在一个固定的时间等待,而implicitlyWait()可以在一个时间范围内等待,称为隐式等待
            driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
            driver.get("https://www.baidu.com/");
            //窗口最大化
            driver.manage().window().maximize();
            //定位所搜框,并输入电影
            driver.findElement(By.cssSelector("#kw")).sendKeys("电影");
            //方法1,强制等待,固定休眠时间设置,sleep()方法以毫秒为单位 ,此类为设置5s等待
            Thread.sleep(5000);
            //定位百度一下按钮
            WebElement searchButton = driver.findElement(By.cssSelector("#su"));
            //点击百度一下
            searchButton.submit();
            //获得当前窗口url
            String parentUrl = driver.getCurrentUrl();
            System.out.println("currenturl:"+parentUrl);
            //获取搜索navigate窗口
            driver.navigate().to("https://www.baidu.com/baidu?tn=monline_3_dg&ie=utf-8&wd=navigate");
            //方法2,显式等待
            (new WebDriverWait(driver, 10)).until(new ExpectedCondition<Boolean>(){
                public Boolean apply(WebDriver d){
                    return d.getTitle().toLowerCase().startsWith("navigate");
                }
            });
            //获得当前窗口url
            String parentUrl2 = driver.getCurrentUrl();
            System.out.println("currenturl:"+parentUrl2);
            //刷新页面
            driver.navigate().refresh();
            //后退到百度首页
            driver.navigate().back();
            //方法2,显式等待,在规定时间内等待 在10秒的范围内 出现.red_box元素就往下执行,如果10秒过后还没出现就跳出
            WebElement element = (new WebDriverWait(driver, 10)).until(ExpectedConditions.presenceOfElementLocated(By.id("su")));
            //前进到搜索navigate窗口
            driver.navigate().forward();
            driver.close();
        } 
        
    }

    注意,如果显式等待搜索的内容不存在,则会跑出异常;

  • 相关阅读:
    你看懂了程序员的这些笑话吗?
    SharePoint 2013 Designer 自己定义操作菜单
    Ubuntu下用glade和GTK+开发C语言界面程序(三)——学习make的使用方法
    【九】注入框架RoboGuice使用:(Your First Injected Service and BroadcastReceiver)
    LeetCode 122 Best Time to Buy and Sell Stock II
    Cocos2d-x 3.1.1 学习日志6--30分钟了解C++11新特性
    HDU 4914 Linear recursive sequence(矩阵乘法递推的优化)
    TS4
    angularjs1-过滤器
    angularjs1- ng-include
  • 原文地址:https://www.cnblogs.com/xiao02fang/p/9372080.html
Copyright © 2020-2023  润新知