Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

syntax for new ExpectedCondition class in Selenium webdriver python

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

like image 748
user2054545 Avatar asked Feb 11 '26 03:02

user2054545


1 Answers

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).

like image 172
Louis Avatar answered Feb 12 '26 19:02

Louis



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!