selenium wait until colour changes

Viewed 682

I need my code to wait until a button has changed colour.
I have used the below code however it sometimes fails because the item is no longer accessible (due to the postback).

To give an overview on what I'm trying to do:

When I click a save button on a website, everything turns grey for an unknown amount of time and then an asp.net postback is used. With selenium I'm trying to wait until a button is no longer grey and the postback has completed. The only way of telling If the postback is finished is if the colour is no longer grey.


Code:

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

Calling the function:

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

HTML:

<input type="submit" name="save_btn" id=" save_btn " class="btn btn-info col-sm-12" value="Home">

Please note, the class name does not change when the buttons are grey

1 Answers

You should use Selenium's Wait which is designed for such a scenario. Their example of a custom expected condition is very close to what you're after. You might even be able to get away with using the presence_of_element_located expected condition with a CSS locator (By.CSS)

You could alter this logic to your own from their example.

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