当我尝试在代码中使用以下语句时,获取对象没有属性“assertEqual”:
self.assertEqual("IRELAND INSTITUTE OF PITTSBURGH", driver.find_element_by_id("cname").get_attribute("value"))
Run Code Online (Sandbox Code Playgroud)
当我在测试用例中使用时,效果很好。我知道将断言放入测试用例中是一个很好的做法。但我想了解为什么这段代码不能在页面中工作,而不是如果我们遵循页面对象模式
我目前正在尝试验证 DOM 中不存在该元素:
我写了这个函数:
def verifyElementNotFound(self, xpath):
element = self.driver.find_element_by_xpath(xpath)
if element.is_displayed():
raise Exception("Element should not be found")
else:
pass
Run Code Online (Sandbox Code Playgroud)
该元素不存在于 dom 中,但它给了我这个错误:
raise exception_class(message, screen, stacktrace)
NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":"//td[.="DO NOT DELETE: Regression Test script 2"]"}
Run Code Online (Sandbox Code Playgroud)
或者我可以使用它,这是一种强大的方法吗?
try:
element = self.driver.find_element_by_xpath(xpath)
if element.is_displayed():
raise Exception("Element should not be found")
except:
pass
Run Code Online (Sandbox Code Playgroud)