Create custom wait until condition in Python

后端 未结 4 1853
失恋的感觉
失恋的感觉 2021-01-04 09:39

I tried to create a function with custom wait condition in Python. However, I get an error:

TypeError: \'bool\' object is not callable

4条回答
  •  没有蜡笔的小新
    2021-01-04 10:13

    The wait.until(..) function from selenium expects a function that it can call. However, you are, effectively, giving it a boolean.

    element = wait.until(waittest(driver, '//div[@id="text"]', "myCSSClass", "false"))
    

    Can be rewritten as:

    value = waittest(driver, '//div[@id="text"]', "myCSSClass", "false")
    element = wait.until(value)
    

    Which makes this more clear - waittest returns a boolean, and wait.until(..) tries to call it as a function - hence 'bool' object is not callable.

    You need to have your custom condition return a function that selenium can call to check if it's true yet. You can do this by defining a function inside your function:

    def waittest(locator, attr, value):
        def check_condition(driver):
            element = driver.find_element_by_xpath(locator)
            if element.get_attribute(attr) == value:
                return element
            else:
                return False
        return check_condition
    

提交回复
热议问题