【发布时间】:2019-11-23 10:51:40
【问题描述】:
我遇到了一个问题,我不确定问题出在哪里。
我为func_tests1.py创建了一个功能测试
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import time
import unittest
class NewVisitorTest(unittest.TestCase):
def setUp(self):
self.browser = webdriver.Firefox()
def tearDown(self):
self.browser.quit()
def test_can_start_a_list_and_retrieve_it(self):
self.browser.get('http://localhost:8000')
#this is where lies the issue
header_text = self.browser.find_element_by_tag('h1')
self.assertIn('To-do', header_text)
self.fail('Finish the test!')
if __name__ == '__main__':
unittest.main(warnings='ignore')
这是我的模板,home.html
<html>
<head>
<title>To-do lists</title>
</head>
<body>
<h1>Your To-do list <h1>
<input id="id_new_item" placeholder="Enter a to-do item"/>
<table id="id_list_table">
</table>
</body>
<!-- -->
</html>
当我执行python3 func_tests1.py 时,出现以下错误:
======================================================================
ERROR: test_can_start_a_list_and_retrieve_it (__main__.NewVisitorTest)
----------------------------------------------------------------------
Traceback (most recent call last):
File "func_tests.py", line 44, in test_can_start_a_list_and_retrieve_it
self.assertIn('To-do',header_text)
File "/usr/lib/python3.6/unittest/case.py", line 1086, in assertIn
if member not in container:
TypeError: argument of type 'FirefoxWebElement' is not iterable
后来在网上搜索了一下,找到了这个fix,正在换行header_text = self.browser.find_elements_by_tag('h1')
到header_text = self.browser.find_elements_by_xpath('h1')
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import time
import unittest
class NewVisitorTest(unittest.TestCase):
def setUp(self):
self.browser = webdriver.Firefox()
def tearDown(self):
self.browser.quit()
def test_can_start_a_list_and_retrieve_it(self):
self.browser.get('http://localhost:8000')
header_text = self.browser.find_elements_by_xpath('h1') #changing to the new method
self.assertIn('To-do', header_text)
self.fail('Finish the test!')
if __name__ == '__main__':
unittest.main(warnings='ignore'
突然间,这个新错误出现了。
======================================================================
FAIL: test_can_start_a_list_and_retrieve_it (__main__.NewVisitorTest)
----------------------------------------------------------------------
Traceback (most recent call last):
File "func_tests.py", line 44, in test_can_start_a_list_and_retrieve_it
self.assertIn('To-do',header_text)
AssertionError: 'To-do' not found in []
----------------------------------------------------------------------
Ran 1 test in 3.369s
谁能告诉我我做错了什么吗?
【问题讨论】:
-
您的
self.browser.title是一个空列表,当您尝试在其中查找“待办事项”(使用assertIn(x,y))时,会出现错误
标签: python django python-3.x selenium