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.
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.
Leave a Reply