深入selenium三種等待方式使用
處理由於網絡延遲造成沒法找到網頁元素
方法一
用time
模塊不推薦使用
用time模塊中的time.sleep來完成等待
from selenium import webdriver
import time
browser = webdriver.Chrome()
response = browser.get('https://www.12306.cn/index/')
#找到登入按鈕點擊
login_button_xpath = '//*[@id="J-header-login"]/a[1]'
login_button = browser.find_element_by_xpath(login_button_xpath)
while not login_button:
login_button = browser.find_element_by_xpath(login_button_xpath)
time.sleep(10)
login_button.click()
方法二
implicitly_wait(隱式等待)
-
隱式等待實際是設置了一個最長等待時間
-
如果在規定時間內網頁加載完成,則執行下一步,否則一直等到時間結束,然后執行下一步。
-
注意點
我們都知道js一般都是放在我們的body的最后進行加載,實際這是頁面上的元素都已經加載完畢,我們卻還在等帶全部頁面加載結束。 -
隱式等待對整個driver周期都起作用,在最開始設置一次就可以了。不要當做固定等待使用,到那都來一下隱式等待。
#隱式等待,失敗概率高,可不用
browser.implicitly_wait(10)
#在10秒內,如果網頁全部加載完成(包含js),則執行下一步,否則一直等到10秒結束后,在執行下一步
方法三
WebDriverWait(顯示等待)(推薦使用)
from selenium.webdriver.support.wait import WebDriverWait
參數
- driver: 傳入WebDriver實例,即我們上例中的driver
- timeout: 超時時間,等待的最長時間
- poll_frequency: 調用until或until_not中的方法的間隔時間,默認是0.5秒
- ignored_exceptions: 忽略的異常,如果在調用until或until_not的過程中拋出這個元組中的異常,則不中斷碼,繼續等待,如果拋出的是這個元組外的異常,則中斷代碼,拋出異常。默認只有NoSuchElementException。
until與until_not
- until:當某元素出現或什么條件成立則繼續執行
- nutil_not:當某元素消失或什么條件不成立則繼續執行
until與until_not里面的兩個參數
- method: 在等待期間,每隔一段時間調用這個傳入的方法,直到返回值不是False
- message: 如果超時,拋出TimeoutException,將message傳入異常
method的設置
必須是含有__call__
的可執行方法。所以我們引用selenium提供的一個模塊
from selenium.webdriver.support import expected_conditions as EC
EC相關匯總
'''隱式等待和顯示等待都存在時,超時時間取二者中較大的'''
locator = (By.ID,'kw')
driver.get(base_url)
WebDriverWait(driver,10).until(EC.title_is(u"百度一下,你就知道"))
'''判斷title,返回布爾值'''
WebDriverWait(driver,10).until(EC.title_contains(u"百度一下"))
'''判斷title,返回布爾值'''
WebDriverWait(driver,10).until(EC.presence_of_element_located((By.ID,'kw')))
'''判斷某個元素是否被加到了dom樹里,並不代表該元素一定可見,如果定位到就返回WebElement'''
WebDriverWait(driver,10).until(EC.visibility_of_element_located((By.ID,'su')))
'''判斷某個元素是否被添加到了dom里並且可見,可見代表元素可顯示且寬和高都大於0'''
WebDriverWait(driver,10).until(EC.visibility_of(driver.find_element(by=By.ID,value='kw')))
'''判斷元素是否可見,如果可見就返回這個元素'''
WebDriverWait(driver,10).until(EC.presence_of_all_elements_located((By.CSS_SELECTOR,'.mnav')))
'''判斷是否至少有1個元素存在於dom樹中,如果定位到就返回列表'''
WebDriverWait(driver,10).until(EC.visibility_of_any_elements_located((By.CSS_SELECTOR,'.mnav')))
'''判斷是否至少有一個元素在頁面中可見,如果定位到就返回列表'''
WebDriverWait(driver,10).until(EC.text_to_be_present_in_element((By.XPATH,"//*[@id='u1']/a[8]"),u'設置'))
'''判斷指定的元素中是否包含了預期的字符串,返回布爾值'''
WebDriverWait(driver,10).until(EC.text_to_be_present_in_element_value((By.CSS_SELECTOR,'#su'),u'百度一下'))
'''判斷指定元素的屬性值中是否包含了預期的字符串,返回布爾值'''
#WebDriverWait(driver,10).until(EC.frame_to_be_available_and_switch_to_it(locator))
'''判斷該frame是否可以switch進去,如果可以的話,返回True並且switch進去,否則返回False'''
#注意這里並沒有一個frame可以切換進去
WebDriverWait(driver,10).until(EC.invisibility_of_element_located((By.CSS_SELECTOR,'#swfEveryCookieWrap')))
'''判斷某個元素在是否存在於dom或不可見,如果可見返回False,不可見返回這個元素'''
#注意#swfEveryCookieWrap在此頁面中是一個隱藏的元素
WebDriverWait(driver,10).until(EC.element_to_be_clickable((By.XPATH,"//*[@id='u1']/a[8]"))).click()
'''判斷某個元素中是否可見並且是enable的,代表可點擊'''
driver.find_element_by_xpath("//*[@id='wrapper']/div[6]/a[1]").click()
#WebDriverWait(driver,10).until(EC.element_to_be_clickable((By.XPATH,"//*[@id='wrapper']/div[6]/a[1]"))).click()
#WebDriverWait(driver,10).until(EC.staleness_of(driver.find_element(By.ID,'su')))
'''等待某個元素從dom樹中移除'''
#這里沒有找到合適的例子
WebDriverWait(driver,10).until(EC.element_to_be_selected(driver.find_element(By.XPATH,"//*[@id='nr']/option[1]")))
'''判斷某個元素是否被選中了,一般用在下拉列表'''
WebDriverWait(driver,10).until(EC.element_selection_state_to_be(driver.find_element(By.XPATH,"//*[@id='nr']/option[1]"),True))
'''判斷某個元素的選中狀態是否符合預期'''
WebDriverWait(driver,10).until(EC.element_located_selection_state_to_be((By.XPATH,"//*[@id='nr']/option[1]"),True))
'''判斷某個元素的選中狀態是否符合預期'''
driver.find_element_by_xpath(".//*[@id='gxszButton']/a[1]").click()
instance = WebDriverWait(driver,10).until(EC.alert_is_present())
'''判斷頁面上是否存在alert,如果有就切換到alert並返回alert的內容'''
By的相關匯總
from selenium.webdriver.common.by import By
BY對於的內容
ID = "id"
XPATH = "xpath"
LINK_TEXT = "link text"
PARTIAL_LINK_TEXT = "partial link text"
NAME = "name"
TAG_NAME = "tag name"
CLASS_NAME = "class name"
CSS_SELECTOR = "css selector"
自定義expected_conditions
class current_url(object):
def __init__(self,current_url):
self.current_url = current_url
def __call__(self, driver):
return self.current_url == driver.current_url
使用舉例
from selenium import webdriver
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
browser = webdriver.Chrome()
response = browser.get('https://www.12306.cn/index/')
login_button_xpath = '//*[@id="J-header-login"]/a[1]'
print(time.time())
#每1秒掃描一次,直到60秒超時后,停止
a=WebDriverWait(browser,60,1).until(EC.presence_of_element_located((By.XPATH,login_button_xpath)))
login_button = browser.find_element_by_xpath(login_button_xpath)
print(time.time())
login_button.click()