zoukankan      html  css  js  c++  java
  • Selenium WebDriver- 隐式等待

    隐式等待是只要有一个元素在设置的时间内没有找到,就会报超时

    隐式等待是一个全局的设置,只要放在找东西语句的前面,它后面的找东西的语句都会默认等待设置的时间(这里是10秒),这是死等,除非立刻找到了,5秒找到了也是会等10秒。

    隐式等待比较耗时,不推荐使用。

    #encoding=utf-8
    import unittest
    import time
    from selenium import webdriver
    from selenium.webdriver import ActionChains
    
    class VisitSogouByIE(unittest.TestCase):
    
        def setUp(self):
            #启动IE浏览器
            #self.driver = webdriver.Firefox(executable_path = "e:\geckodriver")
            self.driver = webdriver.Ie(executable_path = "e:\IEDriverServer")
            
        def test_implictWait(self):
            # 导入异常类
            from selenium.common.exceptions import NoSuchElementException, TimeoutException
            # 导入堆栈类
            import traceback
            url = "http://www.sogou.com"
            # 访问sogou首页
            self.driver.get(url)
            # 通过driver对象implicitly_wait()方法来设置隐式等待时间,最长等待10秒,如果10秒内返回则继续执行后续脚本
            self.driver.implicitly_wait(10)
            try:
                # 查找sogou首页的搜索输入框页面元素
                searchBox = self.driver.find_element_by_id("query")
                # 在搜索输入框中输入“光荣之路自动化测试”
                searchBox.send_keys(u"光荣之路自动化测试")
                # 查找sogou首页搜索按钮页面元素
                click = self.driver.find_element_by_id("stb")
                # 点击搜索按钮
                click.click()
            except (NoSuchElementException, TimeoutException), e:
                # 打印异常的堆栈信息
                traceback.print_exc()
    
    
    
    
    
        def tearDown(self):
            # 退出IE浏览器
            self.driver.quit()
    
    if __name__ == '__main__':
        unittest.main()
  • 相关阅读:
    基础之实战猜年龄游戏
    基本运算符与if while详解:
    while循环练习:
    常量与格式化输出练习
    Flask基础(05)-->路由的基本定义
    Flask基础(04)-->相关配置参数
    Flask基础(03)-->创建第一个Flask程序
    Flask基础(02)-->搭建Flask项目虚拟环境
    Flask基础(01)-->Flask框架介绍
    Flask实战第61天:帖子板块过滤显示
  • 原文地址:https://www.cnblogs.com/qingqing-919/p/8709599.html
Copyright © 2011-2022 走看看