使用 Selenium WebDriver for Python 等待頁面載入。
我們可以使用 Selenium webdriver 等待頁面載入。硒中有一個**同步**概念,描述了隱式和顯式等待。要等到頁面載入,我們將使用針對特定元素的顯式等待概念。
顯式等待被設計成依賴於某個元素預期行為的條件。對於等待頁面載入,我們將使用針對特定元素的預期條件**presence_of_element_loaded**。一旦等待時間結束,將丟擲超時錯誤。
要實現顯式等待條件,我們必須藉助**WebDriverWait** 和 **ExpectedCondition** 類。讓我們檢查頁面上下面元素的存在,並驗證頁面是否已載入。

示例
程式碼實現
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.common.exceptions import TimeoutException
from selenium.webdriver.common.by import By
driver = webdriver.Chrome (executable_path="C:\chromedriver.exe")
driver.get("https://tutorialspoint.tw/about/about_careers.htm")
// presence_of_element_located expected condition wait for 8 seconds
try:
w = WebDriverWait(driver, 8)
w.until(expected_conditions.presence_of_element_located((By.TA
G_NAME,"h1")))
print("Page load happened")
exception TimeException:
print("Timeout happened no page load")
driver.close()輸出

廣告
資料結構
網路
RDBMS
作業系統
Java
iOS
HTML
CSS
Android
Python
C 語言
C++
C#
MongoDB
MySQL
Javascript
PHP