硒的问题-find_elements_by_xpath或find_elements_by_tag

时间:2019-07-14 13:24:30

标签: python django python-3.x selenium

我遇到的问题是我不确定问题在哪里。

我已经为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

有人可以告诉我我在做什么错吗?

2 个答案:

答案 0 :(得分:2)

主要问题是如何定义header_text

header_text = self.browser.find_elements_by_tag('h1')将返回FirefoxWebElement列表

如果只需要第一个元素,则可以使用

header_text = self.browser.find_element_by_tag('h1')  # no plural 

这仅返回第一个匹配的FirefoxWebElement


现在,要预料到下一个错误,如果要比较任何文本,还需要选择此text的{​​{1}}属性。

FirefoxWebElement

答案 1 :(得分:1)

作为替代方案,请考虑在标头中添加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应该是唯一的,而其他标头标签可能会添加。