selenium判断页面加载完成
时间: 2023-07-26 13:26:53 浏览: 126
在使用Selenium时,可以使用Expected Conditions类提供的一些条件来判断页面是否加载完成,例如:
1. 等待页面标题出现:
```
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
wait = WebDriverWait(driver, 10)
title = "Page Title"
wait.until(EC.title_contains(title))
```
2. 等待某个元素出现:
```
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
wait = WebDriverWait(driver, 10)
element = wait.until(EC.presence_of_element_located((By.ID, "element-id")))
```
3. 等待页面加载完成:
```
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
wait = WebDriverWait(driver, 10)
wait.until(EC.presence_of_element_located((By.TAG_NAME, "body")))
```
这些条件可以根据具体的需求进行组合使用,以判断页面是否加载完成。
阅读全文