The expected_conditions module provides methods for judging page elements in 18
import
from selenium.webdriver.support import expected_conditions
View the source code directly
1. title_is(title)(driver)
Whether the page title is the same as the input value
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.titleFirst explain this method of defining Class, in which the object is used to simulate the behavior of the function by calling __call__, so when calling, it can be written directly as title_is(title)(driver)
2. title_contains (title) (driver): whether to contain
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): is it in the 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): whether visible
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()
可查看源码了解具体内容