Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 813
Jagadeesh Kotra Avatar asked Jul 14 '18 08:07

Jagadeesh Kotra


People also ask

Why implicit wait is not recommended?

Hey Aaron, the main disadvantage of implicit wait is that it slows down test performance. The implicit wait will tell to the web driver to wait for certain amount of time before it throws a "No Such Element Exception". The default setting is 0.

Does implicit wait applied to all elements?

Note: Implicitly wait is applied globally which means it is always available for all the web elements throughout the driver instance. It implies if the driver is interacting with 100 elements then, Implicitly wait is applicable for all the 100 elements.

What is the disadvantage of implicit wait?

The main disadvantage of implicit wait is that it slows down test performance. Another disadvantage of implicit wait is: Suppose, you set the waiting limit to be 10 seconds and the elements appears in the DOM in 11 seconds, your tests will be failed because you told it to wait a maximum of 10 seconds.

Is default wait time of implicit wait in Selenium 0?

Implicit Wait in Selenium It's default setting is 0, and the specific wait time needs to be set by the following protocol. To add implicit waits in test scripts, import the following package.


4 Answers

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.

like image 125
Andrei Suvorkov Avatar answered Oct 06 '22 05:10

Andrei Suvorkov


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.

like image 42
Timothy O'Connor Avatar answered Oct 06 '22 05:10

Timothy O'Connor


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.

like image 34
cruisepandey Avatar answered Oct 06 '22 05:10

cruisepandey


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
like image 24
ghoozie Avatar answered Oct 06 '22 05:10

ghoozie