简体   繁体   English

硒的问题-find_elements_by_xpath或find_elements_by_tag

[英]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 . 现在,要预料到下一个错误,如果要比较任何文本,还需要选择此FirefoxWebElementtext属性。

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.

相关问题 Python:Selenium 驱动程序 find_elements_by_xpath:问题 - Python: Selenium Driver find_elements_by_xpath: Issue WebDriver在find_elements_by_xpath上等待 - WebDriverWait on find_elements_by_xpath python/Selenium --&gt; find_elements_by_xpath 方法找不到所有元素 - python/Selenium --> find_elements_by_xpath method not finding all elements Python Selenium 网页抓取:find_elements_by_xpath 返回一个空列表 - Python Selenium Webscraping: find_elements_by_xpath returning an empty list find_elements_by_xpath 的第二次迭代在 selenium python 中给出错误 - Second iterative of find_elements_by_xpath gives error in selenium python Python:硒:find_elements_by_xpath 最大 10 - Python: Selenium: find_elements_by_xpath max 10 了解 find_elements_by_xpath 在 Selenium 中返回的内容 - Understanding what find_elements_by_xpath returns in Selenium 如何提高find_elements_by_xpath的速度 - How to increase the speed of find_elements_by_xpath 使用 selenium find_elements_by_xpath 多次返回相同元素的数组,而不是所有元素 - Using selenium find_elements_by_xpath is returning an array of the same element multiple times rather than all elements 无法使用python中的硒中的find_elements_by_xpath检索元素列表 - Unable retrieve a list of elements using find_elements_by_xpath in selenium using python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM