18-expected_conditions简介

简介: 18-expected_conditions简介

1. 方法概览

方法 说明
title_is 判断当前页面的title是否完全等于(==)预期字符串
title_contains 判断当前页面的title是否包含预期字符串
presence_of_element_located 判断某个元素是否被加到了dom树里,并不代表该元素一定可见
visibility_of_element_located 判断某个元素是否可见. 可见代表元素非隐藏,并且元素的宽和高都不等于0
visibility_of 跟上面的方法一样,直接传定位到的element
presence_of_all_elements_located 判断是否至少有1个元素存在于dom树中。举个例子,如果页面上有n个元素的class都是'column-md-3',那么只要有1个元素存在,返回True
text_to_be_present_in_element 判断某个元素中的text是否,包含预期的字符串
text_to_be_present_in_element_value 判断某个元素中的value属性是否,包含预期的字符串
frame_to_be_available_and_switch_to_it 判断该frame是否可以switch进去
invisibility_of_element_located  判断某个元素中是否不存在于dom树或不可见
element_to_be_clickable  判断某个元素中是否可见并且是否可以点击
staleness_of 等某个元素从dom树中移除
element_to_be_selected  判断某个元素是否被选中了,一般用在下拉列表
element_selection_state_to_be  判断某个元素的选中状态是否符合预期
element_located_selection_state_to_be 跟上面的方法作用一样,只是上面的方法传入定位到的element
alert_is_present 判断页面上是否存在alert
new_window_is_opened 判断窗口是否增加,传入窗口数量
number_of_windows_to_be 期望窗口为多少
frame_to_be_available_and_switch_to_it 判断是否切换到iframe

2. 常用示例

  2.1 标题相关

  • title_is(title):判断当前页面的title是否完全等于(==)预期字符串
  • title_contains(title_part):判断当前页面的title是否包含预期字符串
1. # -*- coding: utf-8 -*-
2. # @Time    : 2021/2/1
3. # @Author  : 大海
4. 
5. import time
6. from selenium import webdriver
7. from selenium.webdriver.support import expected_conditions as ec
8. from selenium.webdriver.support.wait import WebDriverWait
9. 
10. driver = webdriver.Chrome()
11. # 访问我的博客
12. driver.get("https://blog.csdn.net/IT_heima")
13. print(driver.title)
14. title = '小白的博客_爱学习de测试小白_CSDN博客-python+selenium,Jmeter,Vue领域博主'
15. title_part = '小白的博客'
16. # 判断当前页面的title是否完全等于(==)预期字符串
17. WebDriverWait(driver, 10).until(ec.title_is(title))
18. 
19. # 判断当前页面的title是否包含预期字符串
20. # WebDriverWait(driver, 10).until(ec.title_contains(title_part))
21. 
22. driver.close()

  2.2  元素是否可见

  • visibility_of_element_located(locator):判断某个元素是否可见. 可见代表元素非隐藏,并且元素的宽和高都不等于0
1. # -*- coding: utf-8 -*-
2. # @Time    : 2021/2/1
3. # @Author  : 大海
4. 
5. from selenium import webdriver
6. from selenium.webdriver.common.by import By
7. from selenium.webdriver.support import expected_conditions as ec
8. from selenium.webdriver.support.wait import WebDriverWait
9. 
10. driver = webdriver.Chrome()
11. # 访问我的博客
12. driver.get("https://blog.csdn.net/IT_heima")
13. 
14. locator = (By.XPATH, "//*[@class='user-profile-head-name']/div")
15. 
16. WebDriverWait(driver, 10).until(ec.visibility_of_element_located(locator))
17. 
18. driver.close()

 

相关文章
|
5月前
|
JavaScript 程序员 Swift
The compiler is unable to type-check this expression in reasonable time; try breaking up the express
The compiler is unable to type-check this expression in reasonable time; try breaking up the express
64 0
|
BI 数据库
关于 ABAP Flight Reference Scenario
关于 ABAP Flight Reference Scenario
《How Customers Are Using the IBM Data Science Experience-Expected Cases and Not So Expected Ones》电子版地
How Customers Are Using the IBM Data Science Experience-Expected Cases and Not So Expected Ones
《How Customers Are Using the IBM Data Science Experience-Expected Cases and Not So Expected Ones》电子版地
|
Java 测试技术
出现Error creating bean with name与CONDITIONS EVALUATION REPORT问题
出现Error creating bean with name与CONDITIONS EVALUATION REPORT问题
345 0
出现Error creating bean with name与CONDITIONS EVALUATION REPORT问题
|
机器学习/深度学习 算法
Data Structures and Algorithms (English) - 7-28 Review of Programming Contest Rules(30 分)
Data Structures and Algorithms (English) - 7-28 Review of Programming Contest Rules(30 分)
203 0
Data Structures and Algorithms (English) - 7-28 Review of Programming Contest Rules(30 分)
Data Structures and Algorithms (English) - 6-5 Evaluate Postfix Expression(25 分)
Data Structures and Algorithms (English) - 6-5 Evaluate Postfix Expression(25 分)
114 0
PAT (Advanced Level) Practice - 1143 Lowest Common Ancestor(30 分)
PAT (Advanced Level) Practice - 1143 Lowest Common Ancestor(30 分)
116 0
PAT (Advanced Level) Practice - 1130 Infix Expression(25 分)
PAT (Advanced Level) Practice - 1130 Infix Expression(25 分)
121 0
Matlab:成功解决Expression or statements is incorrect--possibly unbalanced (,{,[.
Matlab:成功解决Expression or statements is incorrect--possibly unbalanced (,{,[.
|
JavaScript 前端开发
Guidelines for Function Compute Development - Troubleshoot Timeout Issues
Endless codes and endless bugs When you write code, you may inadvertently introduce some hidden bugs, even if you test a large proportion of the codes to the maximum extent possible.
1631 0