硒,直到颜色改变

paul24682

我需要我的代码来等待按钮变色。
我已经使用了以下代码,但是由于无法再访问该项目,有时有时会失败(由于回发)。

概述一下我要做什么:

当我单击网站上的“保存”按钮时,所有内容在未知的时间内变成灰色,然后使用asp.net回发。对于硒,我试图等待直到按钮不再是灰色并且回发完成为止。判断回发是否完成的唯一方法是颜色是否不再是灰色。


码:

def element_colour_check (self, locator, expected_color):
    # loop to retry if element is still loading
    for f in range(0,10)
        try:
            element = self.driver.find_element_by_id(locator)
            hex_color = Color.from_string(element.value_of_css_property("background-color")).hex

            #check to see if element is correct colour
            if (hex_color == expected_color) == True:
                return True
            else:
                sleep(1)
                continue

        except Exception as e:              
            sleep(1)
            continue
    raise self.insert_error("Element did not change colour")

调用函数:

element_color_loading_check("save_btn", "#17a2b8")

HTML:

<input type="submit" name="save_btn" id=" save_btn " class="btn btn-info col-sm-12" value="Home">

请注意,按钮为灰色时,班级名称不会更改

卢坎

您应该使用Wait针对这种情况设计的Selenium 他们关于自定义预期条件的示例与您所追求的非常接近。您甚至可以通过CSS定位符使用presence_of_element_located预期的条件来摆脱困境By.CSS

您可以从他们的示例中将此逻辑更改为您自己的逻辑。

    if self.css_class in element.get_attribute("class"):
        return element

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章