Python 硒会一直等到颜色改变

Python 硒会一直等到颜色改变,python,python-3.x,selenium,selenium-webdriver,Python,Python 3.x,Selenium,Selenium Webdriver,我需要我的代码等待按钮改变颜色。 我使用了下面的代码,但有时会失败,因为该项目不再可访问(由于回发) 要概述我正在尝试做的事情,请执行以下操作: 当我单击网站上的“保存”按钮时,所有内容都会在未知时间内变为灰色,然后使用asp.net回发。对于selenium,我试图等到按钮不再是灰色并且回发完成。判断回发是否完成的唯一方法是颜色是否不再为灰色 代码: def element_colour_check (self, locator, expected_color): # loop to

我需要我的代码等待按钮改变颜色。
我使用了下面的代码,但有时会失败,因为该项目不再可访问(由于回发)

要概述我正在尝试做的事情,请执行以下操作:

当我单击网站上的“保存”按钮时,所有内容都会在未知时间内变为灰色,然后使用asp.net回发。对于selenium,我试图等到按钮不再是灰色并且回发完成。判断回发是否完成的唯一方法是颜色是否不再为灰色


代码:

def element_colour_check (self, locator, expected_color):
    # loop to retry if element is still loading
    for f in range(0,10)
        try:
            element = self.driver.find_element_by_id(locator)
            hex_color = Color.from_string(element.value_of_css_property("background-color")).hex

            #check to see if element is correct colour
            if (hex_color == expected_color) == True:
                return True
            else:
                sleep(1)
                continue

        except Exception as e:              
            sleep(1)
            continue
    raise self.insert_error("Element did not change colour")
<input type="submit" name="save_btn" id=" save_btn " class="btn btn-info col-sm-12" value="Home">
调用函数:

element_color_loading_check("save_btn", "#17a2b8")
HTML:

def element_colour_check (self, locator, expected_color):
    # loop to retry if element is still loading
    for f in range(0,10)
        try:
            element = self.driver.find_element_by_id(locator)
            hex_color = Color.from_string(element.value_of_css_property("background-color")).hex

            #check to see if element is correct colour
            if (hex_color == expected_color) == True:
                return True
            else:
                sleep(1)
                continue

        except Exception as e:              
            sleep(1)
            continue
    raise self.insert_error("Element did not change colour")
<input type="submit" name="save_btn" id=" save_btn " class="btn btn-info col-sm-12" value="Home">


请注意,当按钮为灰色时,类名不会更改。您应该使用专为这种情况设计的Selenium。他们的自定义预期条件示例与您所追求的非常接近。您甚至可以使用(
By.CSS
)的
presence\u元素(位于
的预期条件)来逃脱

您可以根据他们的示例将此逻辑更改为您自己的逻辑

    if self.css_class in element.get_attribute("class"):
        return element

我以前试过这个,但没法用。我一定是有点打字错误。我现在可以使用以下代码:
def u调用u(self,driver):element=driver.find_element(*self.locator)#如果self.css_类为Color.from_字符串,则查找引用的元素打印(Color.from_字符串(element.value_of_css_属性(“background Color”)).hex)(元素.value\u属性(“背景色”)).hex:返回元素else:返回False