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)