Python & Selenium: Difference Between Driver.Implicitly_Wait() and Time.Sleep()

Selenium implicit and explicit waits not working / has no effect

If you want a pause 10 seconds, use time.sleep():

import time

time.sleep(10) # take a pause 10 seconds

Note: WebDriverWait(driver,10) doesn't work like that. Instead you can use it like this:

from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.wait import WebDriverWait

# this will wait at least 10 seconds until url will contain "your_url"
WebDriverWait(driver, 10).until(EC.url_contains(("your_url")))

but it will not wait exactly 10 seconds, only until expected_conditions will be satisfied.

Also: as source code us tells:

def implicitly_wait(self, time_to_wait):
"""
Sets a sticky timeout to implicitly wait for an element to be found,
or a command to complete. This method only needs to be called one
time per session. To set the timeout for calls to
execute_async_script, see set_script_timeout.

:Args:
- time_to_wait: Amount of time to wait (in seconds)

:Usage:
driver.implicitly_wait(30)
"""
...

driver.implicitly_wait(10) also is used for waiting elements, not to pause script.

PS: it is always a good practice to use WebDriverWait instead of hard pause, because with WebDriverWait your test will be more quickly, since you don't have to wait the whole amount of time, but only until expected_conditions will be satisfied. As I understood, you are just playing arround at the moment, but for the future WebDriverWait is better to use.

implicit or explicit waits in selenium don't work reliably by time.sleep does?

While How to resolve ElementNotInteractableException: Element is not visible in Selenium webdriver? is technically a duplicate, it solves this for Java, and it always annoys me when a duplicate is for a different language, so I'll write this answer for Python.


The ec.presence_of_element_located(...) method just tests for the presence of an element within the document object model. It does not ensure that element is something the user can interact with. Another element might overlap it, or the element might be hidden from view for a brief moment before you call password_input.send_keys(...).

Waiting until the element is "clickable" is usually the best solution:

driver = webdriver.Firefox()
driver.get(f"http://localhost:8888")
wait = WebDriverWait(driver, 10)

# waiting until element is "clickable" means user can interact with
# it, and thus send_keys(...) can simulate keyboard interaction.
password_input = wait.until(ec.element_to_be_clickable((By.ID, "password_input")))

password = "my_password"
password_input.send_keys(password + Keys.RETURN)


Related Topics



Leave a reply



Submit