Search code examples
pythonseleniumwebdriverwait

Selenium wait.until method


Hello fellow programmers, I have a simple question.

Is

wait = WebDriverWait(browser, 10)

equivalent to

time.sleep(10)

?

I'm developing this selenium app and sometimes the server traffic is quite massive which leads to different loading times when moving from page to page. I found time.sleep(N) isn't efficient but if I use the wait.until EC block will the code execute once the condition is met?
It won't waste time if use wait = WebDriverWait(browser, 60) and the page loads in 3 seconds?


Solution

  • time.sleep(10) and wait = WebDriverWait(browser, 10) both are explicit waits.

    The main difference is that

    time.sleep(10) is the worst kind of explicit wait.

    What it means is that it will halt the program execution for the defined time period, in this case, 10 secs.

    Whereas WebDriverWait will look for EC conditions every 500ms if found then return if not retry again and again until a timeout occurs and then throw

    TimeOutException
    

    which is a generic error again.

    Also,

    Since explicit waits allow you to wait for a condition to occur, they make a good fit for synchronising the state between the browser and its DOM, and your WebDriver script.

    here is the official docs for explicit waits.