用Python写UI断言
时间: 2023-11-19 13:06:18 浏览: 34
以下是一个使用Python编写UI断言的示例:
```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.baidu.com")
# 使用显式等待等待搜索框出现
search_box = WebDriverWait(driver, 10).until(
EC.presence_of_element_located((By.NAME, "wd"))
)
# 在搜索框中输入关键字
search_box.send_keys("Python")
# 点击搜索按钮
search_button = driver.find_element_by_id("su")
search_button.click()
# 等待搜索结果出现
search_result = WebDriverWait(driver, 10).until(
EC.presence_of_element_located((By.ID, "content_left"))
)
# 断言搜索结果中是否包含关键字
assert "Python" in search_result.text
# 关闭浏览器
driver.quit()
```
该代码使用了Selenium库来进行UI操作,并使用了显式等待和断言来验证搜索结果是否包含关键字。请注意,这只是一个简单的示例,实际的UI断言可能包含更多的复杂逻辑和条件。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_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)