隐式等待:就是无条件的等待,通过driver对象implicitly_wait()方法来设置隐式等待时间,浏览器会在你自己设定的时间内不断的刷新页面去寻找我们需要的元素,超出设定时间后则抛出找不到元素的异常。
import unittest
import time
from selenium import webdriver
from selenium.webdriver import ActionChains
from selenium.common.exceptions import NoSuchElementException, TimeoutException
import traceback
class VisitSogou(unittest.TestCase):
def setUp(self):
self.driver = webdriver.Ie(executable_path = "d:\\IEDriverServer")
def test_implictWait(self):
url = "http://www.sogou.com"
self.driver.get(url)
self.driver.implicitly_wait(10) #设置隐式等待时间,最长等待10秒,如果10秒内返回则续执行后续脚本,如果定位不到再等待10秒
try:
searchBox = self.driver.find_element_by_id("query")
searchBox.send_keys(u"自动化测试")
click = self.driver.find_element_by_id("stb")
click.click()
except (NoSuchElementExce