使用 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.com/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