📜 ⬆️ ⬇️

Selenium for Python. Chapter 5. Expectations

Continuation of the translation of the unofficial Selenium documentation for Python.
The original can be found here .

Content:


1. Installation
2. First steps
3. Navigation
4. Search for items
5. Expectations
6. Page Objects
7. 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 conditions
There 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.


 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) # seconds driver.get("http://somedomain/url_that_delays_loading") myDynamicElement = driver.find_element_by_id("myDynamicElement") 

Go to the next chapter: Page Objects

PS: 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.

Source: https://habr.com/ru/post/273089/


All Articles