智能等待页面元素(显示的等待同步测试)
1 # -*- coding:utf-8 -*- 2 3 from selenium import webdriver 4 from selenium.webdriver.support.ui import WebDriverWait 5 6 driver = webdriver.Firefox() 7 driver.get('http://demo.tutorialzine.com/2009/09/simple-ajax-website-jquery/demo.html') 8 9 page4button = driver.find_element_by_link_text('Page 4') 10 page4button.click() 11 12 #设置等待时间10秒 13 wait = WebDriverWait(driver, 10) 14 15 try: 16 #在10秒内每500毫秒检查一次元素是否存在, 10秒后元素不存在则提示 tester 17 wait.until(lambda x: x.find_element_by_id('pageContent1'), 'tester') 18 except Exception as e: 19 print e