Python Selenium中等待設(shè)置的實(shí)現(xiàn)
概要
在Web自動(dòng)化測(cè)試中,等待是至關(guān)重要的一環(huán),而Selenium提供了豐富的等待設(shè)置來確保測(cè)試腳本的可靠性和穩(wěn)定性。本文將深入研究Python Selenium中常用的必備等待設(shè)置,包括顯式等待、隱式等待、自定義等待條件等多個(gè)方面。通過詳實(shí)的示例代碼,將為大家提供全面而深入的學(xué)習(xí)體驗(yàn)。
顯式等待
顯式等待是在特定條件下等待某個(gè)元素的出現(xiàn)或者消失。以下是一個(gè)等待元素可點(diǎ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()
隱式等待
隱式等待是在整個(gè)會(huì)話中等待元素出現(xiàn)的最長(zhǎng)時(shí)間。設(shè)置一次即可,全局生效:
from selenium import webdriver # 隱式等待 driver = webdriver.Chrome() driver.implicitly_wait(10) driver.get('https://example.com') element = driver.find_element(By.ID, 'example_id')
自定義等待條件
有時(shí)候我們需要根據(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'))
多重等待條件
有時(shí)我們需要等待多個(gè)條件同時(shí)滿足,可以使用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自動(dòng)化測(cè)試中,頁面的加載狀態(tài)是一個(gè)關(guān)鍵考量因素。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ī)的元素等待,有時(shí)還需要等待元素的出現(xiàn)或者可見性。以下是一個(gè)等待元素存在并可見的示例:
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等待允許在等待期間設(shè)置輪詢條件,增加了等待的靈活性。以下是一個(gè)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加載的等待
對(duì)于異步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))
總結(jié)
在本篇文章中,全面深入地探討了Python Selenium中常用的必備等待設(shè)置,旨在為Web自動(dòng)化測(cè)試提供更為全面和深入的學(xué)習(xí)體驗(yàn)。通過詳實(shí)的示例代碼,深入介紹了顯式等待、隱式等待、自定義等待條件、多重等待條件、頁面加載狀態(tài)的等待、元素存在與可見性等待、Fluent等待以及異步JavaScript加載的等待。這些等待設(shè)置不僅僅是簡(jiǎn)單的時(shí)間延遲,更是在確保腳本執(zhí)行的可靠性和穩(wěn)定性方面的必備工具。
通過顯式等待,能夠精確等待某個(gè)特定條件的出現(xiàn)或消失,提高了腳本的精準(zhǔn)性。隱式等待為整個(gè)會(huì)話提供了最長(zhǎng)等待時(shí)間,全局有效,確保了在查找元素時(shí)的超時(shí)容忍度。自定義等待條件和多重等待條件則進(jìn)一步增強(qiáng)了等待的靈活性,適應(yīng)了更多復(fù)雜的測(cè)試場(chǎng)景。還深入研究了頁面加載狀態(tài)的等待,元素存在與可見性等待,F(xiàn)luent等待以及異步JavaScript加載的等待,涵蓋了更廣泛的測(cè)試需求。這些等待設(shè)置的巧妙應(yīng)用,可以在處理異步加載、提高頁面加載的穩(wěn)定性等方面展現(xiàn)出強(qiáng)大的效果。
到此這篇關(guān)于Python Selenium中等待設(shè)置的實(shí)現(xiàn)的文章就介紹到這了,更多相關(guān)Python Selenium等待設(shè)置內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Python代碼實(shí)現(xiàn)列表分組計(jì)數(shù)
這篇文章主要介紹了Python代碼實(shí)現(xiàn)列表分組計(jì)數(shù),利用Python代碼實(shí)現(xiàn)了使用分組函數(shù)對(duì)列表進(jìn)行分組,并計(jì)算每組的元素個(gè)數(shù)的功能,需要的朋友可以參考一下2021-11-11python調(diào)用opencv實(shí)現(xiàn)貓臉檢測(cè)功能
這篇文章主要介紹了python調(diào)用opencv實(shí)現(xiàn)貓臉檢測(cè)功能,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-01-01numpy 進(jìn)行數(shù)組拼接,分別在行和列上合并的實(shí)例
今天小編就為大家分享一篇numpy 進(jìn)行數(shù)組拼接,分別在行和列上合并的實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2018-05-05用 Python 元類的特性實(shí)現(xiàn) ORM 框架
利用 Python 元類的特性實(shí)現(xiàn) ORM 框架的 insert 功能,通過操作類對(duì)象,對(duì)數(shù)據(jù)表進(jìn)行數(shù)據(jù)增加操作。由于 ORM 比較復(fù)雜,也不要重復(fù)造輪子,就完成一個(gè) insert 相類似的ORM,理解其中的道理即可。2021-05-05python新手學(xué)習(xí)可變和不可變對(duì)象
在本篇文章里小編給大家分享了是一篇關(guān)于python可變對(duì)象和不可變對(duì)象的基礎(chǔ)知識(shí)點(diǎn)內(nèi)容,有需要的朋友們可以參考下。2020-06-06python3+PyQt5實(shí)現(xiàn)拖放功能
這篇文章主要為大家詳細(xì)介紹了python3+PyQt5實(shí)現(xiàn)拖放功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-04-04python PIL Image 圖像處理基本操作實(shí)例
這篇文章主要介紹了python PIL Image 圖像處理基本操作實(shí)例包括圖片加載、灰度圖,圖像通道分離和合并,在圖像上輸出文字,圖像縮放,圖像閾值分割、 二值化,圖像裁剪需要的朋友可以參考下2022-04-04