3

I want to capture whether any button has hover property like background color change when mouse is taken over the button.

i can get the cursor property as:

print webElement.value_of_css_property('cursor')

but cant find how to do it for capturing hover property.

1 Answer 1

4

You can get the background-color, color, text-decoration or similar related CSS properties with value_of_css_property():

webElement.value_of_css_property('background-color')
webElement.value_of_css_property('color')
webElement.value_of_css_property('text-decoration')

Based on this, we can make a function that would get the CSS properties, hover an element and assert the CSS properties changed:

from selenium.webdriver.common.action_chains import ActionChains

def get_properties(element):
    return {
        prop: element.value_of_css_property(prop)
        for prop in ['background-color', 'color', 'text-decoration']
    }

def is_hovered(driver, element):
    properties_before = get_properties(element)

    ActionChains(driver).move_to_element(element).perform()

    properties_after = get_properties(element)
    return properties_before != properties_after

Usage:

button = driver.find_element_by_id("#mybutton")
is_hovered(driver, button)
Sign up to request clarification or add additional context in comments.

3 Comments

How would i know whether the webElement has hovering enabled. i want to return true and false, then capture both the color of buttons before and after hovering.
@aman the idea would be to get the css property values before the hover, then hover the element and assert the css properties changed. Lmk, if you need help implementing it.
yes i need to implement it to capture the buttons with such hover enabled property but cant find a way. please help me on this

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.