2017-02-09 86 views
2

我使用硒Python和尋找一種方式斷言一個元素不存在,是這樣的:斷言的元素不存在蟒蛇硒

assert not driver.find_element_by_xpath("locator").text== "Element Text" 
+0

什麼問題用那行代碼? – BlackBear

+0

@BlackBear那麼,根據超時是什麼,不需要很長時間? –

+0

它失敗,說:無法定位元素 – mike

回答

3

您可以在下面使用:

assert len(driver.find_elements_by_xpath("locator")) < 1 

這應該通過斷言,如果沒有匹配的元素你locator發現或AssertionError如果至少1發現

注意的是,如果產生元素DYNA mically一些JavaScript它可能出現在DOM斷言執行

1

假設你使用py.test在assert您的支票,並要驗證預期異常的消息:

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'