Syntax for new class ExpectedCondition in selenium webdriver python

I am using selenium webdriver with python. I would like to create an explicit wait for the popup to appear. Unfortunately, the general methods of the EC module do not include a turnkey solution for this problem. When searching for many other posts, I understand that I need to write their own EU state .until(new ExpectedCondition() { * the condition and its return arguments *}.

I am having trouble finding documentation regarding the exact syntax for the correct entry. Here's a java example: https://groups.google.com/forum/#!msg/selenium-users/iP174o0ddw4/l83n5C5rcPoJ . Can someone either point to the appropriate documentation (not to wait at all, but to create a new EC), or just help me write a python version if the Java code I just contacted. Many thanks

+3
source share
2 answers

If you want to wait for arbitrary conditions, you do not need to use ExpectedConditionat all. You can simply pass the function to the method until:

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)

condition , :

  • condition , true,

  • 10 ( ).

+5

, , 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())

webdriver/support/expected_conditions.py, alert_is_present :

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

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

, .

0

All Articles