selenium时间等待方法
2019-12-05 15:55:46 167
1.强制等待(sleep)
#-*- coding:utf-8 -*-import timefrom datetime import datetimeprint (datetime.now()) #获取当前时间time.sleep(10) #设置等待时间10sprint(datetime.now()) #再次获取当前时间
2.隐性等待(implicitly_wait())
# -*- coding: utf-8 -*- from selenium import webdriverimport time driver = webdriver.Chrome() driver.implicitly_wait(20) # 隐性等待,最长等30秒 driver.get('https://www.baidu.com')time.sleep(3)driver.quit()
3.显示等待方式(WebDriverWait)
# -*- coding: utf-8 -*- from selenium import webdriver from selenium.webdriver.support.wait import WebDriverWait from selenium.webdriver.support import expected_conditions as EC from selenium.webdriver.common.by import By driver = webdriver.Firefox()driver.get('https://huilansame.github.io') WebDriverWait(driver,20,0.5).until( EC.presence_of_element_located((By.LINK_TEXT, 'CSDN'))) print driver.find_element_by_link_text('CSDN').get_attribute('href')driver.close()
4.总结
睿江云官网链接:https://www.eflycloud.com/home?from=RJ0032