Waits in Selenium Python

These days most of the web apps are using AJAX techniques and angular. When a page is loaded by the browser, the elements within that page may load at different time intervals.

This makes locating elements difficult: if an element is not yet present in the DOM, a locate function will raise an ElementNotVisibleException exception.

Using waits, we can solve this issue. Waiting provides some slack between actions performed - mostly locating an element or any other operation with the element.

Selenium Python provides two types of waits - implicit & explicit. An explicit wait makes WebDriver wait for a certain condition to occur before proceeding further with execution.

An implicit wait makes WebDriver poll the DOM for a certain amount of time when trying to locate an element.

Implicit Wait in Python Selenium

Selenium Python tries to find the element without bothering about whether elements are loaded or not, and selenium python throws NoSuchElementException if element is not present.

Implicitly wait is one of the way to request selenium not throw any exception until provided time.

Default wait time of the selenium is 500 milli-seconds, implicitly wait overrides the default wait time of the selenium python.

If element is found before implicitly wait time, selenium moves to next commands in the program without waiting to complete the implicitly wait time, this wait is also called dynamic wait.

Implicit wait is set for the entire duration of your selenium driver and is set at the start of your program. Most of the automation tester writes the implicit wait after creation of browser object.

Implicit wait tries to find the element in first go, if element is not present implicit wait tries to find the element after 500ms of first polling, if element is not available on second time also then implicit wait tries third time after 500 ms of second try and it goes on till the time reaches the 30 seconds

What it does is, in case while executing, if your selenium python doesn't finds any element then instead of throwing an exception, implicit wait makes your driver to wait for the specified wait time and then try to find the element once again.

If the driver still does not finds the element then it throws exception. Implicit wait does the same for all the elements in your program, so you just have to set it once.


						driver = webdriver.Chrome(executable_path=r'D:\PATH\chromedriver.exe');
						driver.implicitly_wait(30)
						

Explicit Wait in selenium Python

The explicit wait is used to tell the Web Driver to wait for certain conditions or the maximum time limit before throwing an Exception .

We can reuse the WebdriverWait object once we create it.Explicit wait will be applicable for only one line, we have to use it with ExpectedConditions class.

ExplicitWait does not have any effect on findElement and findElements.ExplicitWait also called as WebdriverWait.

WebDriverWait by default calls the ExpectedCondition every 500 milliseconds until it returns successfully.


							Syntax : wait=new WebDriverWait( driver, timeoutInSeconds);
						

Explicit waits are a good way to organize a test script and provide more flexibility, by allowing us to design out tests in a way, where we can wait for some predefined or custom conditions and then carry on with what we want.

Below code waits for the element to become clickable


driver = webdriver.Chrome(executable_path=r'D:\PATH\chromedriver.exe');
driver.get("https://chercher.tech/practice/explicit-wait-sample-selenium-webdriver");
wait = new WebDriverWait(driver, 30 /*timeout in seconds*/);
wait.until(ExpectedConditions.element_to_be_clickable(By.xpath("//button[@id='btn1']"))));


Below are few Expected Conditions :

  • title_is : 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.
  • title_contains : 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
  • presence_of_element_located : 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
  • visibility_of_element_located : An expectation for checking that all elements are present on the DOM of a page and visible. Visibility means that the elements are not only displayed but also has a height and width that is greater than 0. locator - used to find the elements returns the list of WebElements once they are located and visible
  • visibility_of : 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
  • presence_of_all_elements_located : 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
  • text_to_be_present_in_element : An expectation for checking if the given text is present in the specified element. locator, text
  • text_to_be_present_in_element_value : An expectation for checking if the given text is present in the element’s locator, text
  • frame_to_be_available_and_switch_to_it : An expectation for checking whether the given frame is available to switch to. If the frame is available it switches the given driver to the specifie
  • invisibility_of_element_located : An Expectation for checking that an element is either invisible or not present on the DOM. locator used to find the element
  • element_to_be_clickable : An Expectation for checking an element is visible and enabled such that you can click it.
  • staleness_of : Wait until an element is no longer attached to the DOM. element is the element to wait for. returns False if the element is still attached to the DOM, true otherwise.
  • element_to_be_selected : An expectation for checking the selection is selected. element is WebElement object
  • element_located_to_be_selected : An expectation for the element to be located is selected. locator is a tuple of (by, path)
  • element_selection_state_to_be : An expectation for checking if the given element is selected. element is WebElement object is_selected is a Boolean
  • element_located_selection_state_to_be : An expectation to locate an element and check if the selection state specified is in that state. locator is a tuple of (by, path) is_selected is a boolean
  • new_window_is_opened : An expectation that a new window will be opened and have the number of windows handles increase
  • number_of_windows_to_be : An expectation for the number of windows to be a certain value.

About Author

Myself KarthiQ, I am the author of this blog, I know ways to write a good article but some how I donot have the skills to make it to reach people, would you like help me to reach more people By sharing this Article in the social media.

Share this Article Facebook
You can also share knowledge by Adding a topic here


Comment / Suggestion Section
Point our Mistakes and Post Your Suggestions

Recent Addition

new tutorial Protractor Online Training : We have closed registration for training

Please do email to chercher.tech@gmail.com for any queries

If you already registered then please do check your email for the Webex link for the training starting from 15th Nov 2018
 
Join My Facebook Group
Join Group