zoukankan      html  css  js  c++  java
  • selenium测试(Java)-- 隐式等待(十)

    隐式等待相当于设置全局的等待,在定位元素时,对所有元素设置超时时间。

    隐式等待使得WebDriver在查找一个Element或者Element数组时,每隔一段特定的时间就会轮询一次DOM,如果Element或数组没有马上被发现的话。

    默认设置是0。一旦设置,这个隐式等待会在WebDriver对象实例的整个生命周期起作用。一劳永逸。

    package com.test.elementwait;
    
    import java.text.SimpleDateFormat;
    import java.util.Calendar;
    import java.util.concurrent.TimeUnit;
    
    import org.openqa.selenium.By;
    import org.openqa.selenium.NoSuchElementException;
    import org.openqa.selenium.WebDriver;
    import org.openqa.selenium.firefox.FirefoxDriver;
    import org.openqa.selenium.support.ui.WebDriverWait;
    
    public class ImplicitWait {
    
        public static void main(String[] args) {
            WebDriver driver = new FirefoxDriver();
            driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
            driver.get("http://www.baidu.com");
            driver.manage().window().maximize();
    
            try {
                SimpleDateFormat format = new SimpleDateFormat("HH-mm-ss-SSS");
                String time = format.format(Calendar.getInstance().getTime());
                System.out.println("开始的时间: " + time);
    
                driver.findElement(By.id("kw22")).sendKeys("selenium");
    
            } catch (NoSuchElementException e) {
                System.out.println("没有找到元素");
                e.printStackTrace();
            } finally {
                SimpleDateFormat format2 = new SimpleDateFormat("HH-mm-ss-SSS");
                String time2 = format2.format(Calendar.getInstance().getTime());
                System.out.println("结束的时间: " + time2);
                driver.quit();
            }
    
        }
    
    }

    执行结果:

    开始的时间: 23-12-26-775
    没有找到元素
    org.openqa.selenium.NoSuchElementException: Unable to locate element: {"method":"id","selector":"kw22"}
    Command duration or timeout: 10.46 seconds
    For documentation on this error, please visit: http://seleniumhq.org/exceptions/no_such_element.html
    Build info: version: '2.53.0', revision: '35ae25b', time: '2016-03-15 16:57:40'
     8 Driver info: org.openqa.selenium.firefox.FirefoxDriver
    Capabilities [{applicationCacheEnabled=true, rotatable=false, handlesAlerts=true, databaseEnabled=true, version=45.2.0, platform=WINDOWS, nativeEvents=false, acceptSslCerts=true, webStorageEnabled=true, locationContextEnabled=true, browserName=firefox, takesScreenshot=true, javascriptEnabled=true, cssSelectorsEnabled=true}]
    Session ID: dda0673c-da3d-4173-a904-d17148a3e26e
    *** Element info: {Using=id, value=kw22}
        at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
        at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
        at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
        at java.lang.reflect.Constructor.newInstance(Constructor.java:408)
        at org.openqa.selenium.remote.ErrorHandler.createThrowable(ErrorHandler.java:206)
        at org.openqa.selenium.remote.ErrorHandler.throwIfResponseFailed(ErrorHandler.java:158)
        at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:678)
        at org.openqa.selenium.remote.RemoteWebDriver.findElement(RemoteWebDriver.java:363)
        at org.openqa.selenium.remote.RemoteWebDriver.findElementById(RemoteWebDriver.java:413)
        at org.openqa.selenium.By$ById.findElement(By.java:218)
        at org.openqa.selenium.remote.RemoteWebDriver.findElement(RemoteWebDriver.java:355)
        at com.test.elementwait.ImplicitWait.main(ImplicitWait.java:26)
    结束的时间: 23-12-37-273
  • 相关阅读:
    50个好用的前端框架,千万收好以留备用!
    嫦娥五号顺利升空,NASA、欧洲航天局回应
    【电脑故障排查】第1章 BIOS和主板故障
    我的老博客——我在chinaunix的家
    Linux操作系统(第二版)(RHEL 8/CentOS 8)
    Java Web整合开发(21) -- 宏观把握Hibernate
    3 远程仓库
    PHP设计模式-策略模式 转
    ubuntu server设置时区和更新时间
    ubuntu 重启 nginx 失败,* Restarting nginx nginx ...fail!
  • 原文地址:https://www.cnblogs.com/xinxin1994/p/7289548.html
Copyright © 2011-2022 走看看