首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >硒问题- find_elements_by_xpath或find_elements_by_tag

硒问题- find_elements_by_xpath或find_elements_by_tag
EN

Stack Overflow用户
提问于 2019-07-14 13:24:30
回答 2查看 694关注 0票数 2

我有个问题,我不知道问题出在哪里。

我创建了一个名为func_tests1.py的功能测试

代码语言:javascript
运行
复制
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

代码语言:javascript
运行
复制
<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时,我有以下错误:

代码语言:javascript
运行
复制
======================================================================
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')

代码语言:javascript
运行
复制
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'

突然,这个新的错误出现了。

代码语言:javascript
运行
复制
======================================================================
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

有人能告诉我我做错了什么吗?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2019-07-14 14:12:02

主要问题是如何定义header_text

header_text = self.browser.find_elements_by_tag('h1')将返回list of FirefoxWebElement

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

代码语言:javascript
运行
复制
header_text = self.browser.find_element_by_tag('h1')  # no plural 

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

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

代码语言:javascript
运行
复制
header_text = self.browser.find_element_by_tag_name('h1').text

self.assertIn('To-do', header_text) # >>> OK
票数 2
EN

Stack Overflow用户

发布于 2019-07-14 14:54:18

作为另一种选择,可以考虑在标题中添加一个id:

代码语言:javascript
运行
复制
<h1>Your To-do list <h1>

变成了

代码语言:javascript
运行
复制
<h1 id="todolist">Your To-do list <h1>

然后,你可以简单地

代码语言:javascript
运行
复制
self.driver.find_element_by_id("todolist")

可能比xpath/tag解决方案更干净:随着页面的增长,id应该是唯一的,而其他的头标记可能会被添加。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/57027822

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档