[英]issue with selenium - find_elements_by_xpath or find_elements_by_tag
I'm having an issue where I'm not sure where lies the issue. 我遇到的问题是我不确定问题在哪里。
I've created a functional test for, called func_tests1.py
我创建了一个功能测试,称为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')
This is my template, home.html
这是我的模板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>
When I do a python3 func_tests1.py
, I have the following error : 当我执行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
After, doing some search on the internet, I found this fix ,which is changing the line 之后,在互联网上进行了一些搜索,我找到了此修复程序 ,它正在改变这一行
header_text = self.browser.find_elements_by_tag('h1')
to 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'
And suddenly, this new error is appearing. 突然,这个新错误出现了。
======================================================================
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
Can anyone tell me what I'm doing wrong, please? 有人能告诉我我在做什么错吗?
The main issue is how header_text
is defined. 主要问题是如何定义header_text
。
header_text = self.browser.find_elements_by_tag('h1')
will return a list of FirefoxWebElement
. header_text = self.browser.find_elements_by_tag('h1')
将返回FirefoxWebElement
的列表 。
If you only need the first element, you can use 如果只需要第一个元素,则可以使用
header_text = self.browser.find_element_by_tag('h1') # no plural
This one returns only the first matching FirefoxWebElement
. 这个仅返回第一个匹配的FirefoxWebElement
。
Now, to anticipate the next error, if you want to compare any text, you also need to select the text
attribute of this FirefoxWebElement
. 现在,要预料到下一个错误,如果要比较任何文本,还需要选择此FirefoxWebElement
的text
属性。
header_text = self.browser.find_element_by_tag_name('h1').text
self.assertIn('To-do', header_text) # >>> OK
As an alternative, consider adding an id to the header: 作为替代方案,请考虑向标头添加ID:
<h1>Your To-do list <h1>
becomes 变
<h1 id="todolist">Your To-do list <h1>
Then, you can simply 然后,您可以简单地
self.driver.find_element_by_id("todolist")
Might be cleaner than a xpath/tag solution: as the page grows, an id should be unique, whereas other header tags might be added. 可能比xpath / tag解决方案更干净:随着页面的增长,id应该是唯一的,而其他标头标签可能会添加。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.