gpt4 book ai didi

python - 如何创建 Selenium Webdriver 测试以验证元素不存在?

转载 作者:太空狗 更新时间:2023-10-30 01:47:03 24 4
gpt4 key购买 nike

我在 Selenium Webdriver 中为我的 Django 应用程序创建单元测试。

我有一个从数据库中删除主题的 AJAX 方法。我正在尝试弄清楚如何验证已删除的主题不再出现在页面上。

我正在 try catch 当 Webdriver 找不到元素时应该生成的异常:selenium.common.exceptions.NoSuchAttributeException

相反,我看到了一个错误:

*** URLError: <urlopen error timed out>

这是我设置测试的方式:

from selenium import webdriver
from django.utils import unittest

class TestAuthentication(unittest.TestCase):
scheme = 'http'
host = 'localhost'
port = '4444'


def setUp(self):
self._driver = webdriver.Firefox()
self._driver.implicitly_wait(10)

def login_as_Kallie(self):
# Kallie has manual login
self._driver.get('http://localhost:8000/account/signin/')
user = self._driver.find_element_by_id('id_username')
user.send_keys("Kallie")
password = self._driver.find_element_by_id('id_password')
password.send_keys('*********')
submit = self._driver.find_element_by_id('blogin')
submit.click()

def test_Kallie_can_delete_topic(self):
self.login_as_Kallie()
self._driver.find_element_by_link_text("Topic to delete").click()
self._driver.find_element_by_xpath("/html/body/div/div[4]/div/div/div[2]/div/table/tbody/tr/td[2]/div/div[3]/span[5]/a").click()
self._driver.find_element_by_class_name('dialog-yes').click()
self._driver.get("http://localhost:8000/questions/")
# this is the Python exception to catch: selenium.common.exceptions.NoSuchAttributeException
self.assertRaises('selenium.common.exceptions.NoSuchAttributeException', self._driver.find_element_by_link_text("Topic to delete"))

def tearDown(self):
self._driver.quit()

如何测试页面元素是否缺失?

最佳答案

from selenium.common.exceptions import NoSuchElementException

with self.assertRaises(NoSuchElementException):
self._driver.find_element_by_link_text("Topic to delete")

关于python - 如何创建 Selenium Webdriver 测试以验证元素不存在?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9103837/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com