3
我想捕獲是否有任何按鈕具有懸停屬性,如鼠標接管按鈕時背景顏色變化。xpath從python中的selenium webelement捕獲懸停CSS屬性
我能得到光標財產:
print webElement.value_of_css_property('cursor')
但無法找到如何做到這一點捕捉懸停性能。
我想捕獲是否有任何按鈕具有懸停屬性,如鼠標接管按鈕時背景顏色變化。xpath從python中的selenium webelement捕獲懸停CSS屬性
我能得到光標財產:
print webElement.value_of_css_property('cursor')
但無法找到如何做到這一點捕捉懸停性能。
您可以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)
我怎麼會知道webElement是否已啓用徘徊。我想返回true和false,然後在懸停前後捕獲按鈕的顏色。 – aman
@aman這個想法是在懸停之前獲取CSS屬性值,然後懸停該元素並斷言改變了CSS屬性。 Lmk,如果你需要幫助實施它。 – alecxe
是的,我需要實現它捕捉與這樣的懸停啓用屬性的按鈕,但無法找到一種方法。請在此幫助我 – aman