53

Yes I know the question has been asked quite often but I still don't get it. I want to make Selenium wait, no matter what. I tried these methods

driver.set_page_load_timeout(30)
driver.implicitly_wait(90)
WebDriverWait(driver, 10)
driver.set_script_timeout(30)

and other things but it does not work. I need selenium to wait 10 seconds. NO not until some element is loaded or whatever, just wait 10 seconds. I know there is this

try:
   element_present = EC.presence_of_element_located((By.ID, 'whatever'))
   WebDriverWait(driver, timeout).until(element_present)
except TimeoutException:
    print "Timed out waiting for page to load"

I do not want that.

If waiting for some seconds is to much (not achievable) for selenium, what other (python) library's/programs would be capable to achieve this task? With Javas Selenium it does not seem to be a problem...

hansTheFranz
  • 2,511
  • 4
  • 19
  • 35
  • 9
    Why can't you use python `time.sleep(10)` after the page is loaded. Put this sleep method when the next action wants to start – Niranj Rajasekaran Jul 27 '17 at 10:13
  • 1
    @NiranjRajasekaran yes there is the Python way but I wanted to know if selenium can do it somehow. Would like to achieve it over the web driver. But good point :) – hansTheFranz Jul 27 '17 at 10:40

1 Answers1

92

All the APIs you have mentioned is basically a timeout, so it's gonna wait until either some event happens or maximum time reached.

set_page_load_timeout - Sets the amount of time to wait for a page load to complete before throwing an error. If the timeout is negative, page loads can be indefinite.

implicitly_wait - Specifies the amount of time the driver should wait when searching for an element if it is not immediately present.

set_script_timeout - Sets the amount of time to wait for an asynchronous script to finish execution before throwing an error. If the timeout is negative, then the script will be allowed to run indefinitely.

For more information please visit this page. (documention is for JAVA binding, but functionality should be same for all the bindings)

So, if you want to wait selenium (or any script) 10 seconds, or whatever time. Then the best thing is to put that thread to sleep.

In python it would be

import time 
time.sleep(10)

The simplest way to do this in Java is using

try {
    Thread.sleep(10*1000);
} catch (InterruptedException e) {
    e.printStackTrace();
}
ChesuCR
  • 9,352
  • 5
  • 51
  • 114
Gaurang Shah
  • 11,764
  • 9
  • 74
  • 137
  • Whenever I use any waiting mechanism Selenium doesn't run any code after it and some of the code before it. Is there any solution to this? – avisk Sep 04 '22 at 03:25