expected_conditions模块提供了18中判断页面元素的方法
导入
from selenium.webdriver.support import expected_conditions
直接查看源码
1. title_is(title)(driver)
页面title是否和输入值一致
class title_is(object): """An expectation for checking the title of a page. title is the expected title, which must be an exact match returns True if the title matches, false otherwise.""" def __init__(self, title): self.title = title def __call__(self, driver): return self.title == driver.title首先解释一下这种定义Class的方法,其中对象通过调用__call__用来模拟函数的行为,因此调用的时候可以直接写为title_is(title)( driver)
2. title_contains(title)(driver):是否包含
class title_contains(object): """ An expectation for checking that the title contains a case-sensitive substring. title is the fragment of title expected returns True when the title matches, False otherwise """ def __init__(self, title): self.title = title def __call__(self, driver): return self.title in driver.title
3. presence_of_element_located(locator):是否在DOM中
class presence_of_element_located(object): """ An expectation for checking that an element is present on the DOM of a page. This does not necessarily mean that the element is visible. locator - used to find the element returns the WebElement once it is located """ def __init__(self, locator): self.locator = locator def __call__(self, driver): return _find_element(driver, self.locator)
4. visibility_of_element_located(locator):是否可见
class visibility_of_element_located(object): """ An expectation for checking that an element is present on the DOM of a page and visible. Visibility means that the element is not only displayed but also has a height and width that is greater than 0. locator - used to find the element returns the WebElement once it is located and visible """ def __init__(self, locator): self.locator = locator def __call__(self, driver): try: return _element_if_visible(_find_element(driver, self.locator)) except StaleElementReferenceException: return False
5. visibility_of():已知元素是否可见
class visibility_of(object): """ An expectation for checking that an element, known to be present on the DOM of a page, is visible. Visibility means that the element is not only displayed but also has a height and width that is greater than 0. element is the WebElement returns the (same) WebElement once it is visible """ def __init__(self, element): self.element = element def __call__(self, ignored): return _element_if_visible(self.element)
6. presence_of_all_elements_located(locator)(driver):至少有一个存在
class presence_of_all_elements_located(object): """ An expectation for checking that there is at least one element present on a web page. locator is used to find the element returns the list of WebElements once they are located """ def __init__(self, locator): self.locator = locator def __call__(self, driver): return _find_elements(driver, self.locator)
7. visibility_of_any_elements_located(locator)(driver)
8. text_to_be_present_in_element()
9. text_to_be_present_in_element_value()
10. frame_to_be_available_and_switch_to_it()
11. invisibility_of_element_located()
12. element_to_be_clickable()
13. staleness_of()
14. element_to_be_selected()
15. element_located_to_be_selected()
16. element_selection_state_to_be()
17. element_located_selection_state_to_be()
18. alert_is_present()
可查看源码了解具体内容