Webdirverwait 同时并发等待元素
时间: 2023-06-13 16:02:29 浏览: 48
WebDriverWait 可以用来等待网页中的元素加载完成,同时也支持并发等待多个元素,具体可以参考以下示例代码:
```python
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
# 创建 WebDriver 实例
driver = webdriver.Chrome()
# 打开网页
driver.get("https://www.example.com")
# 设置等待时间
wait = WebDriverWait(driver, 10)
# 并发等待多个元素
element1 = wait.until(EC.presence_of_element_located((By.ID, "element1")))
element2 = wait.until(EC.presence_of_element_located((By.ID, "element2")))
element3 = wait.until(EC.presence_of_element_located((By.ID, "element3")))
# 对元素进行操作
element1.click()
element2.send_keys("input text")
element3.clear()
# 关闭浏览器
driver.quit()
```
在上述示例中,我们使用了 `WebDriverWait` 类来等待元素加载完成,同时使用了 `EC.presence_of_element_located` 方法来指定需要等待的元素定位方式,以及 `By.ID` 方法来指定元素的标识符。
注意,在等待元素时,我们可以使用 `until` 方法来等待元素的出现,也可以使用 `until_not` 方法来等待元素的消失。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)