Xpath to grab hover css property from selenium web element in python

I want to capture if any button has a hover property, such as changing the background color when the mouse is hovered over the button.

I can get the cursor property like:

print webElement.value_of_css_property('cursor')

      

but can't find how to do it to grab the hover property.

+3


source to share


1 answer


You can get the background-color

, color

, text-decoration

or similar related CSS properties via 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 create a function that will receive CSS properties, hover over an element, and return CSS properties:



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

      

Using:

button = driver.find_element_by_id("#mybutton")
is_hovered(driver, button)

      

+4


source







All Articles