【问题标题】:xpath for capturing hover css property from selenium webelement in python用于从 python 中的 selenium webelement 捕获悬停 css 属性的 xpath
【发布时间】:2015-08-03 12:51:24
【问题描述】:

我想捕捉当鼠标悬停在按钮上时是否有任何按钮具有悬停属性,例如背景颜色变化。

我可以得到光标属性为:

print webElement.value_of_css_property('cursor')

但找不到捕获悬停属性的方法。

【问题讨论】:

    标签: python css xpath selenium-webdriver


    【解决方案1】:

    您可以使用value_of_css_property() 获取background-colorcolortext-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 是否启用了悬停。我想返回真假,然后捕捉悬停前后按钮的颜色。
    • @aman 的想法是在悬停之前获取 css 属性值,然后悬停元素并断言 css 属性已更改。 Lmk,如果你需要帮助来实现它。
    • 是的,我需要实现它来捕获具有这种悬停启用属性的按钮,但找不到方法。请帮我解决这个问题
    猜你喜欢
    • 2012-04-30
    • 2014-08-16
    • 2020-11-18
    • 2020-04-13
    • 1970-01-01
    • 2012-01-21
    • 1970-01-01
    • 1970-01-01
    • 2021-08-17
    相关资源
    最近更新 更多