Home > Net >  What to use instead of implicitly_wait?
What to use instead of implicitly_wait?

Time:08-12

I use implicit waits in tests, but there is a problem. There are a lot of frames on my project that do not have time to load. As a result, the element is loaded, but the frame is not. Help solve the problem. At the start, I use time.sleep(), but this does not solve my problem.

Code trials:

import time
from selenium import webdriver
from webdriver_manager.chrome import ChromeDriverManager
driver = webdriver.Chrome(ChromeDriverManager().install())
driver.implicitly_wait(40)

CodePudding user response:

It is always much preferable to use WebDriverWait explicit waits, not implicitly_wait.
With WebDriverWait explicit waits you can clearly wait for element presence, visibility and much more.

CodePudding user response:

time.sleep(secs)

time.sleep(secs) suspends the execution of the current thread for the given number of seconds. The argument may be a floating point number to indicate a more precise sleep time. The actual suspension time may be less than that requested because any caught signal will terminate the sleep() following execution of that signal’s catching routine. Also, the suspension time may be longer than requested by an arbitrary amount because of the scheduling of other activity in the system.

Where as implicitly_wait(time_to_wait) is not that effective while dealing with Angular and ReactJS based websites.


Solution

An ideal approach would be to induce WebDriverWait as per the usecase. As an example:

  • To wait for the presence of an element:

    element = WebDriverWait(driver, 5).until(EC.presence_of_element_located((By.ID, "element_ID")))
    
  • To wait for the visibility of an element:

    element = WebDriverWait(driver, 5).until(EC.visibility_of_element_located((By.CSS_SELECTOR, "element_css")))
    
  • To wait for the interactibility of an element:

    WebDriverWait(self.driver, 5).until(EC.element_to_be_clickable((By.XPATH, "element_xpath"))).click()
    
  • To wait for the frame to be available and switch to it of an element:

    WebDriverWait(driver, 5).until(EC.frame_to_be_available_and_switch_to_it((By.CSS_SELECTOR,"iframe.classname#frameID")))
    
  • Note : You have to add the following imports :

    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.common.by import By
    from selenium.webdriver.support import expected_conditions as EC
    
  • Related