Course Content
Testing Tools
Testing Tools
0/1
Sample Python Programs
Sample Python Programs
0/1
Selenium Python First Script
Selenium Python First Script
0/2
HTML Form elements
HTML Form elements
0/1
Handle HTML Frames
Handle HTML Frames
0/2
Handling Dropdown Lists
Handling Dropdown Lists
0/2
Selenium Action class
Selenium Action class
0/1
Python Database Programming
Python Database Programming
0/1
Selenium Python for Beginners [ 2024 ]
About Lesson

Selenium Explicit Wait

The dynamic wait is implemented using the WebDriverWait class. This class consists of the following method:

until()

In this method, we pass an event that may occur and the time units for which we wish to wait for that event to occur in the automation code.

For example:

WebDriverWait(driver, 60).until(
EC.element_to_be_clickable((By.NAME,”btnK”)))

The driver will wait for 60 seconds to find the element. WebDriverWait will wait for an event to occur or result in a time-out exception. The exception we get here is TimedOutException. But if the event occurred, the code will not wait for the entire time to finish in the until wait loop.

Sample Code

# Python Selenium Script - Dynamic Wait
# Python Selenium Tutorials
# www.TestingDocs.com 

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
import unittest

class login(unittest.TestCase):
    def setUp(self):
        self.driver = webdriver.Chrome()
        self.base_url = "https://www.google.com"
        
    def test_example(self):
        driver = self.driver
        driver.get(self.base_url)
        driver.find_element(By.NAME,"q").send_keys("test")
        WebDriverWait(driver, 60).until(
            EC.element_to_be_clickable((By.NAME,"btnK")))
        driver.find_element(By.NAME,"btnK").click()
        WebDriverWait(driver, 60).until(
            EC.element_to_be_clickable((By.LINK_TEXT,"Images")))
        self.assertEqual(driver.title,"test - Google Search")
        
     
    def tearDown(self):
        self.driver.quit()
        
        
if __name__ == '__main__':
    unittest.main()          

Screenshot

Selenium Dynamic Wait Python

 

There are many methods available to help trace an event. In the example code, we have used a method called element_to_be_clickable. This will look for the element for 60 seconds. If it finds the button within 60 seconds, it will exit the until loop and execute the next command, which is clicking on the link. Otherwise, it will time out and throw a TimeOutException.