【问题标题】:issue with selenium - find_elements_by_xpath or find_elements_by_tag硒问题 - find_elements_by_xpath 或 find_elements_by_tag
【发布时间】: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


【解决方案1】:

主要问题是如何定义header_text

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

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

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

这个只返回第一个匹配的FirefoxWebElement


现在,要预测下一个错误,如果要比较任何文本,还需要选择此FirefoxWebElementtext 属性。

header_text = self.browser.find_element_by_tag_name('h1').text

self.assertIn('To-do', header_text) # >>> OK

【讨论】:

    【解决方案2】:

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

    【讨论】:

    • 嗨@serv-inc,这实际上非常非常聪明,使用id来测试html。
    猜你喜欢
    • 2019-08-05
    • 1970-01-01
    • 2017-12-08
    • 2018-10-22
    • 1970-01-01
    • 2017-12-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多