aman
aman

Reputation: 1995

xpath for capturing hover css property from selenium webelement in python

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.

Upvotes: 3

Views: 2553

Answers (1)

alecxe
alecxe

Reputation: 474091

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)

Upvotes: 4

Related Questions