yayu
yayu

Reputation: 8088

Selenium: understanding wait

The most common wait operation I've seen across the web is this

driver.get(url)
# apply a wait now

However, if I am in a REPL, I can see that driver.get is a blocking operation and nothing can be done until it is completed.

>>> driver.get(url)
# this blocks everything else until loading is complete

so how does wait work? If anything, shouldn't waits be declared before a driver.get?

Upvotes: 2

Views: 208

Answers (2)

aberna
aberna

Reputation: 5814

I think you are getting confused by the time needed for accessing an url which is a blocking operation and the command wait implemented in Selenium.

Please refer to the documentation http://selenium-python.readthedocs.org/en/latest/waits.html

Selenium Webdriver provides two types of waits - implicit & explicit. An explicit wait makes WebDriver to wait for a certain condition to occur before proceeding further with executions. An implicit wait makes WebDriver to poll the DOM for a certain amount of time when trying to locate an element.

Code from documentation:

An explicit waits in Selenium is code you define to wait for a certain condition to occur before proceeding further in the code.

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait # available since 2.4.0
from selenium.webdriver.support import expected_conditions as EC # available since 2.26.0

ff = webdriver.Firefox()
ff.get("http://somedomain/url_that_delays_loading")
try:
    element = WebDriverWait(ff, 10).until(EC.presence_of_element_located((By.ID, "myDynamicElement")))
finally:
    ff.quit()

An implicit wait is to tell WebDriver to poll the DOM for a certain amount of time when trying to find an element or elements if they are not immediately available.

from selenium import webdriver
ff = webdriver.Firefox()
ff.implicitly_wait(10) # seconds
ff.get("http://somedomain/url_that_delays_loading")
myDynamicElement = ff.find_element_by_id("myDynamicElement")

Upvotes: 4

Nebril
Nebril

Reputation: 3273

I was usually applying wait not to wait for the site to load, but to have all my js code loaded an executed. It can take several seconds, depending on size and complexity of your scripts.

Upvotes: -1

Related Questions