Selenium waitForElement Selenium waitForElement selenium selenium

Selenium waitForElement


From the Selenium Documentation PDF :

import contextlibimport selenium.webdriver as webdriverimport selenium.webdriver.support.ui as uiwith contextlib.closing(webdriver.Firefox()) as driver:    driver.get('http://www.google.com')    wait = ui.WebDriverWait(driver,10)    # Do not call `implicitly_wait` if using `WebDriverWait`.    #     It magnifies the timeout.    # driver.implicitly_wait(10)      inputElement=driver.find_element_by_name('q')    inputElement.send_keys('Cheese!')    inputElement.submit()    print(driver.title)    wait.until(lambda driver: driver.title.lower().startswith('cheese!'))    print(driver.title)    # This raises    #     selenium.common.exceptions.TimeoutException: Message: None    #     after 10 seconds    wait.until(lambda driver: driver.find_element_by_id('someId'))    print(driver.title)


Selenium 2's Python bindings have a new support class called expected_conditions.py for doing all sorts of things like testing if an element is visible. It's available here.

NOTE: the above file is in the trunk as of Oct 12, 2012, but not yet in the latest download which is still 2.25. For the time being until a new Selenium version is released, you can just save this file locally for now and include it in your imports like I've done below.

To make life a little simpler, you can combine some of these expected condition methods with the Selenium wait until logic to make some very handy functions similar to what was available in Selenium 1. For example, I put this into my base class called SeleniumTest which all of my Selenium test classes extend:

from selenium.common.exceptions import TimeoutExceptionfrom selenium.webdriver.common.by import Byimport selenium.webdriver.support.expected_conditions as ECimport selenium.webdriver.support.ui as ui@classmethoddef setUpClass(cls):    cls.selenium = WebDriver()    super(SeleniumTest, cls).setUpClass()@classmethoddef tearDownClass(cls):    cls.selenium.quit()    super(SeleniumTest, cls).tearDownClass()# return True if element is visible within 2 seconds, otherwise Falsedef is_visible(self, locator, timeout=2):    try:        ui.WebDriverWait(driver, timeout).until(EC.visibility_of_element_located((By.CSS_SELECTOR, locator)))        return True    except TimeoutException:        return False# return True if element is not visible within 2 seconds, otherwise Falsedef is_not_visible(self, locator, timeout=2):    try:        ui.WebDriverWait(driver, timeout).until_not(EC.visibility_of_element_located((By.CSS_SELECTOR, locator)))        return True    except TimeoutException:        return False

You can then use these easily in your tests like so:

def test_search_no_city_entered_then_city_selected(self):    sel = self.selenium    sel.get('%s%s' % (self.live_server_url, '/'))    self.is_not_visible('#search-error')


I have made good experiences using:

  • time.sleep(seconds)
  • webdriver.Firefox.implicitly_wait(seconds)

The first one is pretty obvious - just wait a few seconds for some stuff.

For all my Selenium Scripts the sleep() with a few seconds (range from 1 to 3) works when I run them on my laptop, but on my Server the time to wait has a wider range, so I use implicitly_wait() too. I usually use implicitly_wait(30), which is really enough.

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. The default setting is 0. Once set, the implicit wait is set for the life of the WebDriver object instance.