Using selenium webdriver to wait the attribute of element to change value

后端 未结 2 2131
梦如初夏
梦如初夏 2020-12-09 14:25

I have one element with attribute \"aria-busy\" that changes from true to false when data is in searching and done. How can I use selenium Expected Conditions and Explicit W

相关标签:
2条回答
  • 2020-12-09 14:51

    An Expected Condition is just a callable, you can define it as a simple function:

    def not_busy(driver):
        try:
            element = driver.find_element_by_id("xxx")
        except NoSuchElementException:
            return False
        return element.get_attribute("aria-busy") == "false"
    
    self.wait.until(not_busy)
    

    A bit more generic and modular, though, would be to follow the style of the built-in Expected Conditions and create a class with a overriden __call__() magic method:

    from selenium.webdriver.support import expected_conditions as EC
    
    class wait_for_the_attribute_value(object):
        def __init__(self, locator, attribute, value):
            self.locator = locator
            self.attribute = attribute
            self.value = value
    
        def __call__(self, driver):
            try:
                element_attribute = EC._find_element(driver, self.locator).get_attribute(self.attribute)
                return element_attribute == self.value
            except StaleElementReferenceException:
                return False
    

    Usage:

    self.wait.until(wait_for_the_attribute_value((By.ID, "xxx"), "aria-busy", "false"))
    
    0 讨论(0)
  • 2020-12-09 14:51

    Another approach would involve checking the attribute value with a custom locator where you would not only check the id but also the aria-busy attribute value:

    self.wait.until(EC.presence_of_element_located((By.CSS_SELECTOR, "#xxx[aria-busy=false]")))
    
    0 讨论(0)
提交回复
热议问题