zoukankan      html  css  js  c++  java
  • java selenium (十三) 智能等待页面加载完成

    转自:https://www.cnblogs.com/TankXiao/p/5246557.html

    我们经常会碰到用selenium操作页面上某个元素的时候, 需要等待页面加载完成后, 才能操作。  否则页面上的元素不存在,会抛出异常。  

    或者碰到AJAX异步加载,我们需要等待元素加载完成后, 才能操作 

    selenium 中提供了非常简单,智能的方法,来判断元素是否存在.

    阅读目录

    1. 实例要求 
    2. 隐式等待
    3. 显式等待

    实例要求 

    实例:set_timeout.html 下面的html 代码,  点击click 按钮5秒后, 页面上会出现一个红色的div快, 我们需要写一段自动化脚本智能的去判断这个div是否存在, 然后把这个div 然后高亮。

    复制代码
    <html>
        <head>
            <title>Set Timeout</title>
            <style>
                .red_box {background-color: red; width = 20%; height: 100px; border: none;}
            </style>
            <script>
                function show_div(){
                    setTimeout("create_div()", 5000);
                }
      
                function create_div(){
                    d = document.createElement('div');
                    d.className = "red_box";
                    document.body.appendChild(d);
                }
            </script>
        </head>
        <body>
            <button id = "b" onclick = "show_div()">click</button>
        </body>
    </html>
    复制代码

     

    隐式等待

            WebDriver driver = new FirefoxDriver();
            driver.get("file:///C:/Users/Tank/Desktop/set_timeout.html");    
            
            driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
            WebElement element = driver.findElement(By.cssSelector(".red_box"));      
            ((JavascriptExecutor)driver).executeScript("arguments[0].style.border = "5px solid yellow"",element);  

    其中

    driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);

    意思是, 总共等待10秒, 如果10秒后,元素还不存在,就会抛出异常  org.openqa.selenium.NoSuchElementException

     

    显式等待

    显式等待 使用ExpectedConditions类中自带方法, 可以进行显试等待的判断。 

    显式等待可以自定义等待的条件,用于更加复杂的页面等待条件

    等待的条件

    WebDriver方法

    页面元素是否在页面上可用和可被单击

    elementToBeClickable(By locator)

    页面元素处于被选中状态

    elementToBeSelected(WebElement element)

    页面元素在页面中存在

    presenceOfElementLocated(By locator)

    在页面元素中是否包含特定的文本

    textToBePresentInElement(By locator)

    页面元素值

    textToBePresentInElementValue(By locator, java.lang.String text)

    标题 (title)

    titleContains(java.lang.String title)

    只有满足显式等待的条件满足,测试代码才会继续向后执行后续的测试逻辑

    如果超过设定的最大显式等待时间阈值, 这测试程序会抛出异常。 

    复制代码
        public static void testWait2(WebDriver driver)
        {
            driver.get("E:\StashFolder\huoli_28@hotmail.com\Stash\Tank-MoneyProject\浦东软件园培训中心\我的教材\Selenium Webdriver\set_timeout.html");    
            
            WebDriverWait wait = new WebDriverWait(driver, 20);
            wait.until(ExpectedConditions.presenceOfElementLocated(By.cssSelector(".red_box")));
            WebElement element = driver.findElement(By.cssSelector(".red_box"));      
            ((JavascriptExecutor)driver).executeScript("arguments[0].style.border = "5px solid yellow"",element);  
        }
    复制代码
  • 相关阅读:
    Python解决编码问题: `UnicodeEncodeError: 'gbk' codec can't encode character 'xa0' in position 10
    python中关于windows文件名非法字符的过滤处理
    IDEA 卡住半天,buid(编译)不动——解决办法(适用于maven和gradle)及定位全过程
    Linux 系统常见压缩文件(.deb、.rpm等)解压记录
    Linux Redis 安装异常处理
    从hdfs导入数据到hive表
    Python:Rocketmq消息队列使用
    Linux的nohup命令使用 —— 在服务器后台一直执行程序
    kafka:安装和命令行使用
    kafka报错:kafka.errors.NoBrokers Available,Close of session 0x100457e83740000 java.io.IOException 和 The broker is trying to join the wrong cluster
  • 原文地址:https://www.cnblogs.com/cheese320/p/8967410.html
Copyright © 2011-2022 走看看