selenium webdriver "is_element_present" 与 "driver.find_element_by~" 相同吗?
selenium webdriver "is_element_present" is the same as "driver.find_element_by~"?
我正在 unittest webdriver selenium 中编写测试
如果已经包含 is_element_present 而不是仅 "find_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~',那有什么意义呢?
如果发生这个和这个给我相同的异常,那有什么区别?
isElementPresent
不会抛出与 findElementBy
相同的异常 - 至少永远不会抛出 NoSuchElementException
。您粘贴的代码也隐藏了 NoSuchElementException
- 它只是 returns false.
所以在使用findElementBy
.
时这个异常必须由你来处理
除了例外,return 值也不同。 findElementBy
return 是您稍后可以在代码中使用的第一个匹配项 WebElement
。 isElementPresent
只是检查是否可以在页面上找到指定的元素,returning 一个布尔值。
我正在 unittest webdriver selenium 中编写测试
如果已经包含 is_element_present 而不是仅 "find_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~',那有什么意义呢?
如果发生这个和这个给我相同的异常,那有什么区别?
isElementPresent
不会抛出与 findElementBy
相同的异常 - 至少永远不会抛出 NoSuchElementException
。您粘贴的代码也隐藏了 NoSuchElementException
- 它只是 returns false.
所以在使用findElementBy
.
除了例外,return 值也不同。 findElementBy
return 是您稍后可以在代码中使用的第一个匹配项 WebElement
。 isElementPresent
只是检查是否可以在页面上找到指定的元素,returning 一个布尔值。