【发布时间】:2015-08-03 12:51:24
【问题描述】:
我想捕捉当鼠标悬停在按钮上时是否有任何按钮具有悬停属性,例如背景颜色变化。
我可以得到光标属性为:
print webElement.value_of_css_property('cursor')
但找不到捕获悬停属性的方法。
【问题讨论】:
标签: python css xpath selenium-webdriver
我想捕捉当鼠标悬停在按钮上时是否有任何按钮具有悬停属性,例如背景颜色变化。
我可以得到光标属性为:
print webElement.value_of_css_property('cursor')
但找不到捕获悬停属性的方法。
【问题讨论】:
标签: python css xpath selenium-webdriver
您可以使用value_of_css_property() 获取background-color、color、text-decoration 或类似的相关 CSS 属性:
webElement.value_of_css_property('background-color')
webElement.value_of_css_property('color')
webElement.value_of_css_property('text-decoration')
基于此,我们可以创建一个函数来获取 CSS 属性、悬停元素并断言 CSS 属性已更改:
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
用法:
button = driver.find_element_by_id("#mybutton")
is_hovered(driver, button)
【讨论】: