Jagadeesh Kotra
Jagadeesh Kotra

Reputation: 194

Selenium implicit and explicit waits not working / has no effect

from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait

driver = webdriver.Firefox()
driver.get("https://google.com")

#driver.implicitly_wait(10)

WebDriverWait(driver,10)

print("waiting 10 sec")

driver.quit()

It just quits after page loading. the waits have no effect at all!

demo : https://www.youtube.com/watch?v=GocfsDZFqk8&feature=youtu.be

any help would be highly appreciated.

Upvotes: 14

Views: 19264

Answers (4)

Timothy O'Connor
Timothy O'Connor

Reputation: 61

At least with Python and Chrome driver, my experience is that even when using WebDriverWait you STILL need to use time.sleep for things to work reliably. using implicitly_wait doesnt work. I need to put time.sleep(1) after each operation, or sometimes things don't fire off.

Upvotes: 6

oliverterrell
oliverterrell

Reputation: 81

So we had this same problem, what we did was modify the driver class in selenium with a decorator to sleep for .44 seconds on functions that we modified in the get_driver() function. In this case we wanted to wait to find elements by class, name and id before selenium inputted our desired content. Worked like a charm.

def sleep_decorator(func):
    def wrapper(*args, **kwargs):
        time.sleep(.44)                      # Added side-effect
        return func(*args, **kwargs)         # Modified return
    return wrapper


def get_driver():
    driver = webdriver.Chrome()
    driver.find_element_by_id = sleep_decorator(driver.find_element_by_id)
    driver.find_element_by_name = sleep_decorator(driver.find_element_by_name)
    driver.find_element_by_class_name = sleep_decorator(driver.find_element_by_class_name)
    driver.find_elements_by_id = sleep_decorator(driver.find_elements_by_id)
    driver.find_elements_by_name = sleep_decorator(driver.find_elements_by_name)
    driver.find_elements_by_class_name = sleep_decorator(driver.find_elements_by_class_name)
    return driver

Upvotes: 2

cruisepandey
cruisepandey

Reputation: 29382

By using this WebDriverWait(driver,10) , you have declared the Explicit wait. This is just the declaration , you are not using explicit wait at all.

For make use of Explicit wait, you will have to bind the above code with EC which is Expected condition.

Something like :

wait = WebDriverWait(driver,10)
element = wait.until(EC.element_to_be_clickable((By.NAME, 'q')))
element.send_keys("Hi Google")  

You can refer this link for explicit wait : Explicit wait

Note that time.sleep(10) is worst/extreme type of explicit wait which sets the condition to an exact time period to wait. There are some convenience methods provided that help you write code that will wait only as long as required. WebDriverWait in combination with ExpectedCondition is one way this can be accomplished.

Upvotes: 2

Andrei
Andrei

Reputation: 5647

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.

Upvotes: 18

Related Questions