我有个问题,我不知道问题出在哪里。
我创建了一个名为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')
to
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
有人能告诉我我做错了什么吗?
发布于 2019-07-14 14:12:02
主要问题是如何定义header_text
。
header_text = self.browser.find_elements_by_tag('h1')
将返回list of FirefoxWebElement
。
如果只需要第一个元素,则可以使用
header_text = self.browser.find_element_by_tag('h1') # no plural
这个只返回第一个匹配的FirefoxWebElement
。
现在,要预测下一个错误,如果要比较任何文本,还需要选择这个FirefoxWebElement
的FirefoxWebElement
属性。
header_text = self.browser.find_element_by_tag_name('h1').text
self.assertIn('To-do', header_text) # >>> OK
发布于 2019-07-14 14:54:18
作为另一种选择,可以考虑在标题中添加一个id:
<h1>Your To-do list <h1>
变成了
<h1 id="todolist">Your To-do list <h1>
然后,你可以简单地
self.driver.find_element_by_id("todolist")
可能比xpath/tag解决方案更干净:随着页面的增长,id应该是唯一的,而其他的头标记可能会被添加。
https://stackoverflow.com/questions/57027822
复制相似问题