Python:如何验证页面上的元素已被移除?

2024-10-03 23:28:09 发布

您现在位置:Python中文网/ 问答频道 /正文

我的UI上有一个Clear Filter按钮,被点击后会被删除。在

我的测试用例如下:

1) Click the Clear Filter button

2) Check that the Clear Filter button has been removed from the UI

在下面的代码中,我单击按钮,然后我再次尝试单击它,因为它不存在测试用例应该通过的,但我试图通过再次单击来验证失败,这似乎不是解决它的最佳方法。。。在

 log_page.clear_filter_bttnclick()

    try:
        if log_page.clear_filter_bttnclick():
            testrailFunctions.failed()
    except NoSuchElementException:
        testrailFunctions.passed()

我可以在这里使用try/except来验证按钮是否已删除,或者是否有更好的方法来确认Clear Filter按钮不再存在于UI上?在

干杯


Tags: the方法loguipage测试用例buttonfilter
1条回答
网友
1楼 · 发布于 2024-10-03 23:28:09

您可以检查元素是否存在。在

在python中,它应该执行如下操作:

from selenium.common.exceptions import NoSuchElementException        
def check_exists_by_xpath(xpath):
    try:
        webdriver.find_element_by_xpath(xpath)
    except NoSuchElementException:
        return False
    return True

资料来源:

Python - Selenium WebDriver - Checking element exists

或者

方法返回WebElement对象,而不是对象列表。你必须:

^{pr2}$

抱歉:

verify element existence selenium

相关问题 更多 >