Continuation of the translation of the unofficial Selenium documentation for Python.
The original can be found
here .
Content:
1.
Installation2.
First steps3.
Navigation4.
Search for items5. Expectations
6. Page
Objects7. WebDriver API
8. Appendix: Frequently Asked Questions
5. Expectations
Nowadays, most web applications use
AJAX technology. When the page is loaded in the browser, the elements on this page can be loaded at different time intervals. This makes it difficult to find elements, if the element is not present in the
DOM , an ElementNotVisibleException exception is thrown. Using expectations, we can solve this problem. Waiting gives a certain time interval between the actions performed - the search for an element or any other operation with an element.
Selenium WebDriver provides two types of expectations - implicit (implicit) and explicit (explicit). Explicit waiting causes WebDriver to expect a certain condition to occur before performing actions. Implicit wait causes WebDriver to poll the DOM for a certain amount of time when it tries to find an item.
')
5.1 Explicit expectations
Explicit waiting is the code by which you determine what necessary condition must occur in order for the further code to be executed. The worst example of such a code is the use of the time.sleep () command, which sets the exact wait time. There are more convenient methods to help you write code that expects as much as you need. WebDriverWait in combination with ExpectedCondition is one such way.
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 driver = webdriver.Firefox() driver.get("http://somedomain/url_that_delays_loading") try: element = WebDriverWait(driver, 10).until( EC.presence_of_element_located((By.ID, "myDynamicElement")) ) finally: driver.quit()
This code will wait 10 seconds before it throws a TimeoutException exception, or if it finds an element in those 10 seconds, it will return it. WebDriverWait defaults to ExpectedCondition every 500 milliseconds until a successful return is received. A successful return for ExpectedCondition is of type Boolean and returns true, or returns not null for all other ExpectedCondition types.
Expected conditionsThere are some conditions that are often found in the automation of websites. The following are the implementations of each. Bundles in Selenium Python provide some convenient methods, so you don’t have to write the expected_condition class yourself or create your own utility package.
- title_is
- title_contains
- presence_of_element_located
- visibility_of_element_located
- visibility_of
- presence_of_all_elements_located
- text_to_be_present_in_element
- text_to_be_present_in_element_value
- frame_to_be_available_and_switch_to_it
- invisibility_of_element_located
- element_to_be_clickable - it is Displayed and Enabled.
- staleness_of
- element_to_be_selected
- element_located_to_be_selected
- element_selection_state_to_be
- element_located_selection_state_to_be
- alert_is_present
from selenium.webdriver.support import expected_conditions as EC wait = WebDriverWait(driver, 10) element = wait.until(EC.element_to_be_clickable((By.ID,'someid')))
The expected_conditions module already contains a set of predefined conditions for working with WebDriverWait.
5.2 Implicit expectations
Implicit waiting tells WebDriver to poll the DOM for a certain amount of time when it tries to find an element or elements that are not available at that moment. The default value is 0. After installation, implicit wait is set for the lifetime of the instance of the WebDriver object.
from selenium import webdriver driver = webdriver.Firefox() driver.implicitly_wait(10)
Go to the next chapter: Page
ObjectsPS:
For some reason, the user
penguino stopped making further translations of the documentation, so I decided to take the liberty to translate the next chapter.