Create custom wait until condition in Python
WebDriverWait(driver, 10).until()
accepts a callable object which will accept an instance of a webdriver (driver
is our example) as an argument. The simplest custom wait, which expects to see 2 elements, will look like
WebDriverWait(driver, 10).until(
lambda wd: len(wd.find_elements(By.XPATH, 'an xpath')) == 2
)
The waittest
function has to be rewritten as:
class waittest:
def __init__(self, locator, attr, value):
self._locator = locator
self._attribute = attr
self._attribute_value = value
def __call__(self, driver):
element = driver.find_element_by_xpath(self._locator)
if element.get_attribute(self._attribute) == self._attribute_value:
return element
else:
return False
And then it can be used as
element = WebDriverWait(driver, 10).until(
waittest('//div[@id="text"]', "myCSSClass", "false")
)
what I really end up to do is using lambda
self.wait.until(lambda x: waittest(driver, "//div[@id="text"]", "myCSSClass", "false"))