Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

selenium.wait_for_condition equivalent in Python bindings for WebDriver

I'm moving some tests from Selenium to the WebDriver. My problem is that I can't find an equivalent for selenium.wait_for_condition. Do the Python bindings have this at the moment, or is it still planned?

like image 989
hwiechers Avatar asked Sep 12 '10 10:09

hwiechers


People also ask

What is Selenium Python bindings?

What are Selenium-Python Bindings? Selenium Python bindings provide an easy to use API to write functional acceptance tests using Selenium WebDriver. The Selenium Python API allows users to access various functionalities of the Selenium WebDriver in an intuitive way.

Is WebDriver interface in Python?

Web Drivers for Using Selenium With PythonSelenium requires a web driver, which will help it interface with the browser that you want to run your tests on. For example, the Firefox browser uses a geckodriver, which you need to install in the right path. The path can be /usr/bin or /usr/local/bin.

Is WebDriver a class or interface in Python?

Selenium WebDriver is an interface that defines a set of methods. However, implementation is provided by the browser specific classes. Some of the implementation classes are AndroidDriver , ChromeDriver , FirefoxDriver , InternetExplorerDriver , IPhoneDriver , SafariDriver etc.

Can you use Selenium with Python?

Selenium is an open source automation testing tool that supports a number of scripting languages like Python, C#, Java, Perl, Ruby, JavaScript, etc.


2 Answers

Currently it isn't possible to use wait_for_condition with WebDriver. The python selenium code does provide the DrivenSelenium class for accessing the old selenium methods, but it can't do wait_for_condition. The selenium wiki has some info on that.

Your best bet is to use the WebDriverWait class. This is a helper class that periodically executes a function waiting for it to return True. My general usage is

driver = webdriver.Firefox()
driver.get('http://example.com')
add = driver.find_element_by_id("ajax_button")
add.click()
source = driver.page_source

def compare_source(driver):
    try:
        return source != driver.page_source
    except WebDriverException:
        pass

WebDriverWait(driver, 5).until(compare_source)
# and now do some assertions

This solution is by no means ideal.. The try/except is necessary for situations where the page request/response cycle is delayed waiting for some ajax activity to complete. If compare_source get's called in the midst of the request/response cycle it'll throw a WebDriverException.

The test coverage for WebDriverWait is also helpful to look at.

like image 81
schickm Avatar answered Oct 25 '22 08:10

schickm


Here's my version of Greg Sadetsky's answer, put into a function:

def click_n_wait(driver, button, timeout=5):
    source = driver.page_source
    button.click()
    def compare_source(driver):
        try:
            return source != driver.page_source
        except WebDriverException:
            pass
    WebDriverWait(driver, timeout).until(compare_source)

It clicks the button, waits for the DOM to change and then returns.

like image 35
Nimo Avatar answered Oct 25 '22 09:10

Nimo