Wait until a specific element is present in Selenium Python

Hello programmers, in this tutorial we will understand about ‘waits‘ using selenium Python.

Selenium is used for user interactions with web browsers. It has a wide range of tools that helps in the automation of browsers.

Here we will see a demo on how to wait for a specific element present in the website using selenium Python.

Installation of selenium

Type the below command in your command prompt to download the module.

python -m pip install -U selenium

Waiting for an element in selenium Python

When a page is loaded in the browser, not all the elements in the website load at the same time. They load at different time intervals. This makes it difficult for finding a particular element at the start using an automation tool. The exception ElementNotVisibleException will be raised if the element is not found by the automation tool.

Selenium Webdriver has two types of waits:

  • Implicit wait
  • Explicit wait

An explicit wait makes the webdriver wait until a certain condition occurs and an implicit wait makes the webdriver wait for a certain amount of time for a particular element that is not immediately available to locate.

Usage of wait methods in selenium Python

We need to install the chrome driver and then copy the file location of the executable file to the system path.

Below given is the illustration for using an implicit wait in a website using selenium Python.

#Importing Necessary Libraries
from selenium import webdriver
import time

#implicit wait function
def seleniumImplicitWait():
    #creating a webdriver object
    driver = webdriver.Chrome(executable_path='C:/path/to/dir/chromedriver.exe')
    driver.maximize_window() #maximize window size
    driver.get("https://www.codespeedy.com/") #opening the url
    driver.implicitly_wait(15) #wait for 15 seconds for finding the element
    ele = driver.find_element_by_link_text("Programming Blog") #finding the element
    time.sleep(10)
    ele.click() #clicking on the element
    time.sleep(20)

if __name__ == "__main__":
    seleniumImplicitWait() #call the function

Output

The chrome browser is triggered and the following URL is opened. The program
waits for 15 seconds and then opens the ‘Programming Blog’ page.

Explanation

The ‘seleniumImplicitWait()’ function is triggered. Inside the function, a driver object is created for the chrome browser. The website is opened using the ‘.get()’ method. Next, the ‘.implicit_wait()’ method is called for 15 seconds to find the required element. After it is found, the following element is clicked to give the corresponding result. For this task, the selenium module had been imported.

Below given is the illustration for using an explicit wait in a website using selenium Python.

#Importing Necessary Libraries
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 time

#explicit wait function
def seleniumExplicitWait():
    #creating a webdriver object
    driver = webdriver.Chrome(executable_path='C:/path/to/dir/chromedriver.exe')
    driver.maximize_window() #maximize window size

    driver.get("https://www.codespeedy.com/") #opening the url
    ele = WebDriverWait(driver, 10).until( #using explicit wait for 10 seconds
        EC.presence_of_element_located((By.LINK_TEXT, "Programming Blog")) #finding the element
    )
    ele.click() #clicking on the element
    time.sleep(20)

if __name__ == "__main__":
    seleniumExplicitWait() #call the function

Output

The chrome browser is triggered and the following URL is opened. The program
waits for 10 seconds and then opens the ‘Programming Blog’ page.

Explanation

The ‘seleniumExplicitWait()’ function is triggered. A driver object is created in the chrome browser. The website is opened using the ‘.get()’ method. Next, the ‘WebDriverWait()’ method is called for 10 seconds until the element is found on the webpage. After it is found, the following element is clicked to give the corresponding result. For this task, the selenium module had been imported.

For further details  Click a particular element in Selenium Python

Leave a Reply

Your email address will not be published. Required fields are marked *