python webdriver判斷網頁是否加載完成


'''
三種等待方法:
1.強制等待sleep(xx)
強制等待,不管你瀏覽器是否加載完了,程序都得等待,時間一到,繼續執行下面的代碼,作為調試很有用,有時候也可以在代碼里這樣等待,不過不建議總用這種等待方式,太死板,嚴重影響程序執行速度。

2.隱性等待implicitly_wait(xx)
隱形等待是設置了一個最長等待時間,如果在規定時間內網頁加載完成,則執行下一步,否則一直等到時間截止,然后執行下一步。有些時候我們不需要等到所有元素加載完成,我想等我要的元素出來之后就立馬執行下一步,那就應該用顯性等待。

3.顯性等待WebDriverWait(driver,timeout,xx),配合該類的until()和until_not()方法
程序每隔xx秒看一眼,如果條件成立了,則執行下一步,否則繼續等待,直到超過設置的最長時間,然后拋出TimeoutException。

判斷條件:expected_conditions
導入模塊:selenium.webdriver.support.expected_conditions
'''
#coding=utf-8
from selenium import webdriver
from time import sleep
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()
url = "https://www.baidu.com"
timeout = 20
driver.implicitly_wait(10)   #隱式等待和顯示等待都存在時,超時時間取二者中較大的
driver.get(url)
#強制等待:強制等3秒再執行下一步
#sleep(3)

#隱性等待:隱形等待是設置了一個最長等待時間,如果在規定時間內網頁加載完成,則執行下一步,否則一直等到時間截止,然后執行下一步
#隱性等待對整個driver的周期都起作用,所以只要設置一次即可
#driver.implicitly_wait(30)

#顯性等待:WebDriverWait,配合該類的until()和until_not()方法,就能夠根據判斷條件而進行靈活地等待了。
#它主要的意思就是:程序每隔xx秒看一眼,如果條件成立了,則執行下一步,否則繼續等待,直到超過設置的最長時間,然后拋出TimeoutException
#until方法:當某元素出現或什么條件成立則繼續執行
#until_not方法:當某元素消失或什么條件不成立則繼續執行

#expected_conditions:selenium的一個模塊,其中包含一系列可用於判斷的條件:
#以下兩個條件類驗證title,驗證傳入的參數title是否等於或包含於driver.title
#title_is
#title_contains

#以下兩個條件驗證元素是否出現,傳入的參數都是元組類型的locator,如(By.ID, ‘kw’)
#顧名思義,一個只要一個符合條件的元素加載出來就通過;另一個必須所有符合條件的元素都加載出來才行
#presence_of_element_located
#presence_of_all_elements_located

#以下兩個條件判斷某段文本是否出現在某元素中,一個判斷元素的text,一個判斷元素的value
#text_to_be_present_in_element
#text_to_be_present_in_element_value

#以下條件判斷frame是否可切入,可傳入locator元組或者直接傳入定位方式:id、name、index或WebElement
#frame_to_be_available_and_switch_to_it

#以下條件判斷是否有alert出現
#alert_is_present

#以下條件判斷元素是否可點擊,傳入locator
#element_to_be_clickable

#以下四個條件判斷元素是否被選中,第一個條件傳入WebElement對象,第二個傳入locator元組
#第三個傳入WebElement對象以及狀態,相等返回True,否則返回False
#第四個傳入locator以及狀態,相等返回True,否則返回False
#element_to_be_selected
#element_located_to_be_selected
#element_selection_state_to_be
#element_located_selection_state_to_be

#最后一個條件判斷一個元素是否仍在DOM中,傳入WebElement對象,可以判斷頁面是否刷新了
#staleness_of

locator = (By.LINK_TEXT,"設置")
try:
    print(WebDriverWait(driver,timeout).until(EC.title_is(u"百度一下,你就知道")))
    print(WebDriverWait(driver,timeout).until(EC.title_contains(u"知道")))
    WebDriverWait(driver,timeout).until(EC.presence_of_element_located(locator))
    WebDriverWait(driver,timeout).until(EC.visibility_of_element_located((By.ID,'su')))
    WebDriverWait(driver,timeout).until(EC.text_to_be_present_in_element((By.XPATH,"//*[@id='u1']/a[8]"),u"設置"))
    WebDriverWait(driver,timeout).until(EC.text_to_be_present_in_element_value((By.CSS_SELECTOR,'#su'),u'百度一下'))
    WebDriverWait(driver,timeout).until(EC.invisibility_of_element_located((By.CSS_SELECTOR,'#swfEveryCookieWrap')))
    WebDriverWait(driver,timeout).until(EC.element_to_be_clickable((By.XPATH,"//*[@id='u1']/a[8]"))).click()
    #WebDriverWait(driver,timeout).until(EC.element_to_be_selected(driver.find_element(By.XPATH,"//*[@id='nr']/option[1]")))
    '''
    instance = WebDriverWait(driver,timeout).until(EC.alert_is_present())
    print(instance.text())
    instance.accept()
    '''
    '''判斷某個頁面上是否存在alert,如果有alert則返回alert內容,並通過'''
    print(driver.find_element_by_link_text("設置").get_attribute('href'))
    # 打印當前頁面title
    print(driver.title)
    # 打印當前頁面URL
    print(driver.current_url)
finally:
    #關閉當前頁面
    driver.close()
#退出瀏覽器
#driver.quit()

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM