软件测试学习笔记丨Selenium学习笔记:常用页面信息对比方法expected_conditions

时间:2024-10-25 12:08:37
import time from selenium import webdriver from selenium.webdriver.common.by import By from selenium.webdriver.support import expected_conditions driver = webdriver.Chrome() driver.get('https://work.weixin.qq.com/') time.sleep(5) locator = (By.CSS_SELECTOR, '[data-js-click-report-log="79506103,web_first_khq_click"]') element = driver.find_element(*locator) EC = expected_conditions # expected_conditions简写EC # 对比页面标题:对比driver.title与传入值 # EC.title_is('标题内容') # 判断:'标题内容' == driver.title # EC.title_contains('标题内容') # 判断:'标题内容' in driver.title # 对比url # EC.url_to_be('url') # 判断:'url' == driver.current_url # EC.url_changes('url') # 判断:'url' != driver.current_url # EC.url_contains('url') # 判断:'url' in driver.current_url # EC.url_matches('正则表达式') # 判断:driver.current_url是否匹配正则表达式 # 对比窗口 # n = 3 # EC.number_of_windows_to_be(n) # 判断:当前窗口数量 = n # 对比frame # EC.frame_to_be_available_and_switch_to_it(locator) # 元素所在frame可切入则切入并返回True,否则返回False # 对比弹窗 # EC.alert_is_present() # 有弹窗则返回弹窗对象,否则返回False # 判断元素是否出现:指元素在网页中是否存在,不关注元素状态 # EC.presence_of_element_located(locator) # 返回元素对象 # EC.presence_of_all_elements_located(locator) # 返回元素列表 # 判断元素状态 # EC.element_to_be_clickable(locator) # 元素可点击则返回元素对象,否则返回False # EC.element_to_be_clickable(element) # 元素可点击则返回元素对象,否则返回False # EC.element_to_be_selected(element) # 元素是否被选中 # EC.element_located_to_be_selected(locator) # 元素是否被选中 # EC.element_selection_state_to_be(element, True) # 元素是否被选中 # EC.element_located_selection_state_to_be(locator, True) # 元素是否被选中 # EC.staleness_of(element) # 元素不存在 # 判断元素是否可见(注意传入参数) # EC.visibility_of(element) # 元素可见则返回元素对象,否则返回False # EC.visibility_of_element_located(locator) # 元素可见则返回元素对象,否则返回False # EC.visibility_of_all_elements_located(locator) # 全部可见则返回元素列表,否则返回False # EC.visibility_of_any_elements_located(locator) # 返回可见元素列表,无则返回空列表 # EC.invisibility_of_element(element)) # 元素不可见则返回元素对象,否则返回False # EC.invisibility_of_element_located(locator) # 元素不可见则返回元素对象,否则返回False # 对比元素属性值 # EC.text_to_be_present_in_element(locator, '文本内容') # '文本内容' in element.text # EC.text_to_be_present_in_element_value(locator, '文本内容') # '文本内容' in element.get_attribute("value") # EC.text_to_be_present_in_element_attribute(locator, '属性', '属性值') # '属性值' in element.get_attribute("属性") # EC.element_attribute_to_include(locator, '属性') # element.get_attribute("属性") is not None # 组合判断 # li = [EC.title_is('标题内容'), EC.url_to_be('url')] # expected_conditions列表:['EC1', 'EC2', '...'] # EC.any_of(li) # 或:任意一个满足则返回True,否则返回False # EC.all_of(li) # 且:全部都满足则返回True,否则返回False # EC.none_of(li) # 非:全部都不满足则返回True,否则返回False driver.quit()