from selenium.common.exceptions import NoSuchElementException
def test_element_does_not_exist(self):
with self.assertRaises(NoSuchElementException):
browser.find_element_by_xpath("locator")
import pytest
def test_foo():
with pytest.raises(Exception) as excinfo:
x = driver.find_element_by_xpath("locator").text
assert excinfo.value.message == 'Unable to locate element'
您可以使用以下选项:
如果找不到与您的
locator
匹配的元素,或者至少找到1个AssertionError
匹配的元素,则应该传递断言注意,如果元素是由某些
JavaScript
动态生成的,则它可能出现在DOM
断言执行后的DOM
中。在这种情况下,您可以实现ExplicitWait:从selenium.webdriver.common.by导入 从selenium.webdriver.support.ui导入WebDriverWait 从selenium.webdriver.support将预期条件导入为EC
在这种情况下,如果元素在10秒内出现在DOM中,我们将得到
AssertionError
如果要检查元素是否不存在,最简单的方法是使用with语句。
假设您正在使用py.test签入
assert
,并且希望验证预期异常的消息:相关问题 更多 >
编程相关推荐