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()

 

posted @ 2018-04-03 17:01  小七Libra  阅读(257)  评论(0编辑  收藏  举报