Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Selenium waitForElement

How do I write the function for Selenium to wait for a table with just a class identifier in Python? I'm having a devil of a time learning to use Selenium's Python webdriver functions.

like image 581
Breedly Avatar asked Oct 16 '11 01:10

Breedly


People also ask

How do you wait for a specific element in Selenium?

We can wait until an element is present in Selenium webdriver. This can be done with the help of synchronization concept. We have an explicit wait condition where we can pause or wait for an element before proceeding to the next step. The explicit wait waits for a specific amount of time before throwing an exception.

How wait until elements are visible?

Selenium: Waiting Until the Element Is Visiblevar wait = new WebDriverWait(driver, TimeSpan. FromSeconds(20)); As you can see, we give the WebDriverWait object two parameters: the driver itself and a TimeSpan object that represents the timeout for trying to locate the element.

How do I wait for an element to appear in Selenium Python?

To wait until element is present, visible and interactable with Python Selenium, we can use the wait. until method. Then we call wait.

Which Selenium wait is better?

The best practice to wait for a change in Selenium is to use the synchronization concept. The implicit and explicit waits can be used to handle a wait. The implicit is a global wait applied to every element on the page. The default value of implicit wait is 0.


2 Answers

From the Selenium Documentation PDF :

import contextlib import selenium.webdriver as webdriver import selenium.webdriver.support.ui as ui  with 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) 
like image 106
unutbu Avatar answered Oct 16 '22 02:10

unutbu


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 TimeoutException from selenium.webdriver.common.by import By import selenium.webdriver.support.expected_conditions as EC import selenium.webdriver.support.ui as ui  @classmethod def setUpClass(cls):     cls.selenium = WebDriver()     super(SeleniumTest, cls).setUpClass()  @classmethod def tearDownClass(cls):     cls.selenium.quit()     super(SeleniumTest, cls).tearDownClass()  # return True if element is visible within 2 seconds, otherwise False def 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 False def 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') 
like image 29
Dave Koo Avatar answered Oct 16 '22 02:10

Dave Koo