Using Selenium implicit wait in Python

The selenium package is used for automation and testing using Python scripts. We can use it to access various elements in the web page and use them.

There are many methods in this package that can be used to retrieve elements based on different attributes. When a page is loaded, some elements are dynamically retrieved.

These elements may load differently compared to other elements.


Using Selenium implicit wait in Python

raises ElementNotVisibleException if we try to get an element that is not available. This happens because the element is defined in the source but is not yet visible in the DOM.

For this we can use implicit wait. By using selenium's implicit wait, we can tell the webdriver object to wait for the required amount of time before throwing an exception.

If the required element is not found during this time, an exception will be thrown.

We use the implicitly_wait() function to set the implicit wait time. This function is used with webdriver to specify an implicit wait time.

Time is specified in seconds.

See the code below.

from selenium import webdriver
driver = webdriver.Chrome(r'C:/path/to/chromedriver.exe')
driver.implicitly_wait(10)
driver.get("https://www.sample.org/")
e = driver.find_element_by_id("some_form")

In the above example, we are using the webdriver object to redirect to the web page and trying to retrieve the element using the find_element_by_id() function. This function will find elements whose id attribute matches the provided value.

Since this is a dynamic element, we use the implicitly_wait() method to specify an implicit time of ten seconds to ensure the element has time to load.

Guess you like

Origin blog.csdn.net/fengqianlang/article/details/134242081