python selenium中文文檔-頁面等待方法

python selenium中文文檔-頁面等待方法

5. Waits 等待

目前,大多數Web應用程序都在使用AJAX技術。 當瀏覽器加載頁面時,該頁面中的元素可能以不同的時間間隔加載。 這使定位元素變得困難:如果DOM中尚未存在元素,則locate函數將引發ElementNotVisibleException異常。 使用等待,我們可以解決DOM中元素未加載成功,使用元素定位元素或任何其他操作失敗的問題。

Selenium Webdriver提供兩種類型的等待 - 隱式和顯式。 顯式等待使WebDriver等待某個條件發生,然後再繼續執行。 在嘗試查找元素時,隱式等待會使WebDriver輪詢DOM一段時間。

5.1. Explicit Waits 顯示等待

顯式等待是你定義的代碼,用於在進一步執行代碼之前等待某個條件發生。 這種情況的極端情況是time.sleep(),它將條件設置為等待的確切時間段。 提供一種等待方法,可以幫助你編寫僅在某個條件下,需要等待的代碼。 WebDriverWait與ExpectedCondition相結合就是一種可以實現的方法。

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
driver = webdriver.Firefox()
driver.get("http://somedomain/url_that_delays_loading")
try:

element = WebDriverWait(driver, 10).until(
EC.presence_of_element_located((By.ID, "myDynamicElement"))
)
finally:
driver.quit()

這會在,它發現元素在10秒內返回,會返回成功,否則10秒後,拋出TimeoutException異常。 默認情況下,WebDriverWait每500毫秒調用一次ExpectedCondition,直到成功返回。 對於所有其他ExpectedCondition類型,ExpectedCondition類型的布爾返回true或非null返回值成功返回。

Expected Conditions

預期條件

在自動化Web瀏覽器時,常常會出現一些常見情況。 下面列出的是每個的名稱。 Selenium Python綁定提供了一些常規的方法,因此你不必自己編寫expected_condition類或為它們創建自己的實用程序包。

· title_is

· title_contains

· presence_of_element_located

· visibility_of_element_located

· visibility_of

· presence_of_all_elements_located

· text_to_be_present_in_element

· text_to_be_present_in_element_value

· frame_to_be_available_and_switch_to_it

· invisibility_of_element_located

· element_to_be_clickable

· staleness_of

· element_to_be_selected

· element_located_to_be_selected

· element_selection_state_to_be

· element_located_selection_state_to_be

· alert_is_present

from selenium.webdriver.support import expected_conditions as EC
wait = WebDriverWait(driver, 10)
element = wait.until(EC.element_to_be_clickable((By.ID, 'someid')))

expected_conditions模塊包含一組用於WebDriverWait的預定義條件。

Custom Wait Conditions

自定義等待條件

如果前面的系統方法都不符合你的要求,你還可以創建自定義等待條件。 可以使用帶有__call__方法的類創建自定義等待條件,該方法在條件不匹配時返回False。

class element_has_css_class(object):
"""An expectation for checking that an element has a particular css class.
locator - used to find the element
returns the WebElement once it has the particular css class
"""
def __init__(self, locator, css_class):
self.locator = locator
self.css_class = css_class
def __call__(self, driver):
element = driver.find_element(*self.locator) # Finding the referenced element
if self.css_class in element.get_attribute("class"):

return element
else:
return False
# Wait until an element with id='myNewInput' has class 'myCSSClass'
wait = WebDriverWait(driver, 10)
element = wait.until(element_has_css_class((By.ID, 'myNewInput'), "myCSSClass"))

5.2. Implicit Waits隱式等待

隱式等待告訴WebDriver在嘗試查找不能立即可用的任何元素(或元素)時輪詢DOM一段時間。 默認設置為0.設置後,將為WebDriver對象的生命週期設置隱式等待。

from selenium import webdriver
driver = webdriver.Firefox()
driver.implicitly_wait(10) # seconds
driver.get("http://somedomain/url_that_delays_loading")
myDynamicElement = driver.find_element_by_id("myDynamicElement")

--------------------------------------------------------

請關注pyhelloworld,瞭解更多精彩內容


分享到:


相關文章: