selenium webdriver“is_element_present”与“相同”driver.find_元素~"?

2024-04-19 08:56:29 发布

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

我正在用unittest webdriver selenium编写测试

使用is-element-_-present,而不是“find\u-element-by~”,这有什么意义呢?在

def is_element_present(self, how, what):
        try: self.driver.find_element(by=how, value=what)
        except NoSuchElementException, e: return False
        return True

我的意思是,每当我使用“is_element_present”时,它都会经过“find_element_by~”,那么这有什么意义呢?在

这和这给了我同样的例外,如果发生了,有什么区别?在


Tags: selfbyreturnisdefseleniumelementunittest
1条回答
网友
1楼 · 发布于 2024-04-19 08:56:29

isElementPresent不会引发与findElementBy相同的异常-至少不会抛出NoSuchElementException。粘贴的代码也隐藏了NoSuchElementException它只是返回false。在

因此,在使用findElementBy时,必须处理此异常。在

除了异常之外,返回值也不同。findElementBy返回第一个匹配的WebElement,稍后可以在代码中使用。isElementPresent只是检查在页面上是否可以找到指定的元素,返回一个布尔值。在

相关问题 更多 >