Python Selenium中等待設置的實現(xiàn)
概要
在Web自動化測試中,等待是至關重要的一環(huán),而Selenium提供了豐富的等待設置來確保測試腳本的可靠性和穩(wěn)定性。本文將深入研究Python Selenium中常用的必備等待設置,包括顯式等待、隱式等待、自定義等待條件等多個方面。通過詳實的示例代碼,將為大家提供全面而深入的學習體驗。
顯式等待
顯式等待是在特定條件下等待某個元素的出現(xiàn)或者消失。以下是一個等待元素可點擊的示例:
from selenium.webdriver.common.by import By from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.support import expected_conditions as EC # 顯式等待 element = WebDriverWait(driver, 10).until( EC.element_to_be_clickable((By.ID, 'example_id')) ) element.click()
隱式等待
隱式等待是在整個會話中等待元素出現(xiàn)的最長時間。設置一次即可,全局生效:
from selenium import webdriver # 隱式等待 driver = webdriver.Chrome() driver.implicitly_wait(10) driver.get('https://example.com') element = driver.find_element(By.ID, 'example_id')
自定義等待條件
有時候我們需要根據(jù)自定義的條件等待,可以使用expected_conditions
中的expected_conditions
類:
from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.support import expected_conditions as EC # 自定義等待條件 class ElementHasText: def __init__(self, locator, text_): self.locator = locator self.text = text_ def __call__(self, driver): element_text = EC._find_element(driver, self.locator).text return self.text in element_text element_locator = (By.ID, 'example_id') wait = WebDriverWait(driver, 10) wait.until(ElementHasText(element_locator, 'Expected Text'))
多重等待條件
有時我們需要等待多個條件同時滿足,可以使用expected_conditions
中的and_
或or_
:
from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.support import expected_conditions as EC # 多重等待條件 element_locator = (By.ID, 'example_id') wait = WebDriverWait(driver, 10) wait.until(EC.and_( EC.element_to_be_clickable(element_locator), EC.visibility_of_element_located(element_locator) ))
頁面加載狀態(tài)的等待
在Web自動化測試中,頁面的加載狀態(tài)是一個關鍵考量因素。Selenium提供了expected_conditions
中的document_to_be_ready_state
來等待頁面加載完成:
from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.support import expected_conditions as EC # 頁面加載狀態(tài)的等待 wait = WebDriverWait(driver, 10) wait.until(EC.document_to_be_ready_state('complete'))
元素存在與可見性等待
除了常規(guī)的元素等待,有時還需要等待元素的出現(xiàn)或者可見性。以下是一個等待元素存在并可見的示例:
from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.support import expected_conditions as EC # 元素存在與可見性等待 element_locator = (By.ID, 'example_id') wait = WebDriverWait(driver, 10) element = wait.until(EC.visibility_of_element_located(element_locator))
Fluent等待
Fluent等待允許在等待期間設置輪詢條件,增加了等待的靈活性。以下是一個Fluent等待的示例:
from selenium.webdriver.common.by import By from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.support.ui import FluentWait from selenium.webdriver.support import expected_conditions as EC from selenium.common.exceptions import TimeoutException # Fluent等待 wait = WebDriverWait(driver, 10) element = FluentWait(driver, timeout=10, poll_frequency=1, ignored_exceptions=[TimeoutException]) \ .until(lambda x: x.find_element(By.ID, 'example_id'))
異步JavaScript加載的等待
對于異步JavaScript加載的元素,可以使用expected_conditions
中的invisibility_of_element_located
來等待其加載完成:
from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.support import expected_conditions as EC # 異步JavaScript加載的等待 element_locator = (By.ID, 'async_element') wait = WebDriverWait(driver, 10) wait.until(EC.invisibility_of_element_located(element_locator))
總結
在本篇文章中,全面深入地探討了Python Selenium中常用的必備等待設置,旨在為Web自動化測試提供更為全面和深入的學習體驗。通過詳實的示例代碼,深入介紹了顯式等待、隱式等待、自定義等待條件、多重等待條件、頁面加載狀態(tài)的等待、元素存在與可見性等待、Fluent等待以及異步JavaScript加載的等待。這些等待設置不僅僅是簡單的時間延遲,更是在確保腳本執(zhí)行的可靠性和穩(wěn)定性方面的必備工具。
通過顯式等待,能夠精確等待某個特定條件的出現(xiàn)或消失,提高了腳本的精準性。隱式等待為整個會話提供了最長等待時間,全局有效,確保了在查找元素時的超時容忍度。自定義等待條件和多重等待條件則進一步增強了等待的靈活性,適應了更多復雜的測試場景。還深入研究了頁面加載狀態(tài)的等待,元素存在與可見性等待,F(xiàn)luent等待以及異步JavaScript加載的等待,涵蓋了更廣泛的測試需求。這些等待設置的巧妙應用,可以在處理異步加載、提高頁面加載的穩(wěn)定性等方面展現(xiàn)出強大的效果。
到此這篇關于Python Selenium中等待設置的實現(xiàn)的文章就介紹到這了,更多相關Python Selenium等待設置內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
numpy 進行數(shù)組拼接,分別在行和列上合并的實例
今天小編就為大家分享一篇numpy 進行數(shù)組拼接,分別在行和列上合并的實例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-05-05