Xpath to grab hover css property from selenium web element in python
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 to share