WeirdGeek

Data Science | Machine Learning | Automation

  • Data Analytics
  • Python
  • Data Science
  • Google Apps Script
  • Machine Learning
  • Artificial Intelligence
  • SQL Server

13/03/2020 By WeirdGeek Leave a Comment

Function to wait till element loaded in the Web page using Selenium

Here in this post we will write a function to wait till element loaded in the Web page using Selenium in Python to help you to automate your task.

When you are working with selenium and python to automate some task related to web pages, you have faced a situation where you got some error because the element you are trying to location is still not loaded properly. Then you manually add some time to load the element. But what if you can create a function that will do the work for you and wait till it gets the required element.

Function to wait till element loaded in the Web page using Selenium in Python

Here is the below function to wait till element loaded in the Web page using Selenium in Python:

from selenium.common.exceptions import TimeoutException
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

def wait(element):
    try:
        element_present = EC.presence_of_element_located((By.XPATH,element))
        
        WebDriverWait(browser, 20).until(element_present)
        print("Element Found")
    except TimeoutException:
        print ("Timed out waiting for page to load")

The above code will wait till the expected condition is met. In this case the code will look for the XPath element and wait till 20 seconds and if it didn’t find anything during that duration it throws the exception. If you want to modify or increase the time to wait till the element loads, increase it in the WebDriverWait.

Hope this helps you out. Head over to the link, If you want to read more article about the Python. Also check the official link of Python Selenium for more understanding. Feel free to reach me via the comments section.

Related posts:

  1. Python Script to transfer file from one location to another
  2. 5 comparisons between generator function and generator expression in Python
  3. Reading 15 most common file formats used in Data Science (using Python)

Filed Under: Python Tagged With: Python, Python Script, Selenium

Leave a Reply Cancel reply

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

Subscribe to my Blog !!

Enter your email below to subscribe my blog and get the latest post right in your inbox.

  • Home
  • Terms
  • Privacy
  • Contact Us

Copyright © 2025 · WeirdGeek · All trademarks mentioned are the property of their respective owners.