3

I am using selenium webdriver with python. I would like to create an explicit wait for a popup window to appear. Unfortunately, the common methods of the EC module do not include a ready-made solution for this problem. Searching many other posts, I gather that I have to write my own EC condition, with .until(new ExpectedCondition() { * the condition and its return arguments *}.

I am having trouble finding documentation regarding the exact syntax to use to write this properly. There is a java example here: https://groups.google.com/forum/#!msg/selenium-users/iP174o0ddw4/l83n5C5rcPoJ. Could somebody either point to the relevant documentation (not on waits generally, but on creating new EC), or simply help me write the python version if the java code I just linked to. Thanks a lot

2 Answers 2

5

If you want to wait for arbitrary conditions, you don't have to use ExpectedCondition at all. You can just pass a function to the until method:

from selenium.webdriver.support.ui import WebDriverWait

def condition(driver):
    ret = False
    # ...
    # Actual code to check condition goes here and should
    # set `ret` to a truthy value if the condition is true
    # ...
    return ret

WebDriverWait(driver, 10).until(condition)

The code above will call condition repeatedly until either of the following is true:

  • condition returns a value that evaluates true,

  • 10 seconds have elapsed (in which case an exception is raised).

Sign up to request clarification or add additional context in comments.

1 Comment

Just wanted to point out the source code with sample conditions: github.com/SeleniumHQ/selenium/blob/master/py/selenium/…
0

I haven't had occassion to try it, but I think you could use EC.alert_is_present:

import selenium.webdriver.support.expected_conditions as EC
import selenium.webdriver.support.ui as UI

wait = UI.WebDriverWait(driver, 10)
alert = wait.until(EC.alert_is_present())

Inside webdriver/support/expected_conditions.py, alert_is_present is defined like this:

class alert_is_present(object):
    """ Expect an alert to be present."""
    def __init__(self):
        pass

    def __call__(self, driver):
        try:
            alert = driver.switch_to_alert()
            alert.text
            return alert
        except NoAlertPresentException:
            return False

You could write it more simply as

def alert_is_present(driver):
    try:
        alert = driver.switch_to_alert()
        alert.text
        return alert
    except NoAlertPresentException:
        return False

This might give you an idea of how such a condition could be written.

2 Comments

It worked great when defining my new condition as a class, as you showed above for the alert_is_present method. Thanks!
I deleted the issue I mentioned because it was a simple error in the way I had defined my class.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.