• <i id='ZQWtP'><tr id='ZQWtP'><dt id='ZQWtP'><q id='ZQWtP'><span id='ZQWtP'><b id='ZQWtP'><form id='ZQWtP'><ins id='ZQWtP'></ins><ul id='ZQWtP'></ul><sub id='ZQWtP'></sub></form><legend id='ZQWtP'></legend><bdo id='ZQWtP'><pre id='ZQWtP'><center id='ZQWtP'></center></pre></bdo></b><th id='ZQWtP'></th></span></q></dt></tr></i><div id='ZQWtP'><tfoot id='ZQWtP'></tfoot><dl id='ZQWtP'><fieldset id='ZQWtP'></fieldset></dl></div>
    1. <tfoot id='ZQWtP'></tfoot>

        <small id='ZQWtP'></small><noframes id='ZQWtP'>

          <bdo id='ZQWtP'></bdo><ul id='ZQWtP'></ul>
      1. <legend id='ZQWtP'><style id='ZQWtP'><dir id='ZQWtP'><q id='ZQWtP'></q></dir></style></legend>

        TypeError:“WebElement"对象不可下标

        时间:2023-07-04
          • <i id='VGqX3'><tr id='VGqX3'><dt id='VGqX3'><q id='VGqX3'><span id='VGqX3'><b id='VGqX3'><form id='VGqX3'><ins id='VGqX3'></ins><ul id='VGqX3'></ul><sub id='VGqX3'></sub></form><legend id='VGqX3'></legend><bdo id='VGqX3'><pre id='VGqX3'><center id='VGqX3'></center></pre></bdo></b><th id='VGqX3'></th></span></q></dt></tr></i><div id='VGqX3'><tfoot id='VGqX3'></tfoot><dl id='VGqX3'><fieldset id='VGqX3'></fieldset></dl></div>

            <small id='VGqX3'></small><noframes id='VGqX3'>

            <tfoot id='VGqX3'></tfoot>

              <tbody id='VGqX3'></tbody>
            <legend id='VGqX3'><style id='VGqX3'><dir id='VGqX3'><q id='VGqX3'></q></dir></style></legend>

              • <bdo id='VGqX3'></bdo><ul id='VGqX3'></ul>

                1. 本文介绍了TypeError:“WebElement"对象不可下标的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我尝试使用 Python 在 Spotify Web Player 上按下重播按钮,但出现此错误.如何在网络播放器中按下按钮?

                  replay = driver.find_element_by_xpath("""/html/body/div[2]/div/div[4]/div[3]/footer/div/div[2]/div/div[1]/div[5]/button""")[0]重播.click()

                  错误:

                  replay = driver.find_element_by_xpath("""/html/body/div[2]/div/div[4]/div[3]/footer/div/div[2]/div/div[1]/div[5]/button""")[0]TypeError:WebElement"对象不可下标

                  解决方案

                  这个错误信息...

                  TypeError 'WebElement' 对象不可下标

                  ...表示您已将索引附加到 WebElement 不支持.


                  分析

                  只有 list 元素可以被索引.但是,在这行代码中:

                  replay = driver.find_element_by_xpath("""/html/body/div[2]/div/div[4]/div[3]/footer/div/div[2]/div/div[1]/div[5]/button""")[0]

                  driver.find_element_by_xpath("""/html/body/div[2]/div/div[4]/div[3]/footer/div/div[2]/div/div[1]/div[5]/button"""") 将始终返回 single WebElement.因此,您无法通过任何索引访问元素,例如[0][1]等作为index只能与list关联.p>


                  解决方案

                  有两种方法可以解决这个问题.

                  • 在第一种方法中,您可以删除 index,即 [0],在这种情况下 replay 将是分配有通过 定位器策略 标识的第一个匹配元素,如下所示:

                    replay = driver.find_element_by_xpath("""/html/body/div[2]/div/div[4]/div[3]/footer/div/div[2]/div/div[1]/div[5]/button"""")

                  • 在另一种方法中,您可以使用 find_elements_by_xpath() 创建一个 list,而不是使用 find_element_by_xpath() 并访问List 中的第一个元素,使用索引 [0] 如下:

                    replay = driver.find_elements_by_xpath("""/html/body/div[2]/div/div[4]/div[3]/footer/div/div[2]/div/div[1]/div[5]/button""")[0]


                  参考

                  您可以在以下位置找到一些相关讨论:

                  • 发生异常:TypeError 'WebElement' 对象不可下标

                  I try to press the Replay button at Spotify Web Player with Python, but get this error. How can I press buttons in a webplayer?

                  replay = driver.find_element_by_xpath("""/html/body/div[2]/div/div[4]/div[3]/footer/div/div[2]/div/div[1]/div[5]/button""")[0]
                  replay.click()
                  

                  Error:

                  replay = driver.find_element_by_xpath("""/html/body/div[2]/div/div[4]/div[3]/footer/div/div[2]/div/div[1]/div[5]/button""")[0]
                  TypeError: 'WebElement' object is not subscriptable
                  

                  解决方案

                  This error message...

                  TypeError 'WebElement' object is not subscriptable
                  

                  ...implies that you have attached an index to a WebElement which is not supported.


                  Analysis

                  Only list elements can be indexed. However, in this line of code:

                  replay = driver.find_element_by_xpath("""/html/body/div[2]/div/div[4]/div[3]/footer/div/div[2]/div/div[1]/div[5]/button""")[0]
                          
                  

                  driver.find_element_by_xpath("""/html/body/div[2]/div/div[4]/div[3]/footer/div/div[2]/div/div[1]/div[5]/button""") would always return a single WebElement. Hence you can't access the element through any index, e.g. [0], [1], etc as an index can be associated only with a list.


                  Solution

                  There are two approaches to solve the issue.

                  • In the first approach, you can remove the index, i.e. [0], and in that case replay will be assigned with the first matched element identified through locator strategy as follows:

                    replay = driver.find_element_by_xpath("""/html/body/div[2]/div/div[4]/div[3]/footer/div/div[2]/div/div[1]/div[5]/button""")
                    

                  • In the other approach, instead of using find_element_by_xpath() you can create a list using find_elements_by_xpath() and access the very first element from the List using the index [0] as follows:

                    replay = driver.find_elements_by_xpath("""/html/body/div[2]/div/div[4]/div[3]/footer/div/div[2]/div/div[1]/div[5]/button""")[0]
                    


                  Reference

                  You can find a couple of relevant discussions in:

                  • Exception has occurred: TypeError 'WebElement' object is not subscriptable

                  这篇关于TypeError:“WebElement"对象不可下标的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:如何在 python 中使用 selenium 将整个文本发送到文本区域而不是逐行发送? 下一篇:自动单击网页中的按钮

                  相关文章

                2. <i id='EftpL'><tr id='EftpL'><dt id='EftpL'><q id='EftpL'><span id='EftpL'><b id='EftpL'><form id='EftpL'><ins id='EftpL'></ins><ul id='EftpL'></ul><sub id='EftpL'></sub></form><legend id='EftpL'></legend><bdo id='EftpL'><pre id='EftpL'><center id='EftpL'></center></pre></bdo></b><th id='EftpL'></th></span></q></dt></tr></i><div id='EftpL'><tfoot id='EftpL'></tfoot><dl id='EftpL'><fieldset id='EftpL'></fieldset></dl></div>

                      • <bdo id='EftpL'></bdo><ul id='EftpL'></ul>
                      <tfoot id='EftpL'></tfoot>

                    1. <small id='EftpL'></small><noframes id='EftpL'>

                      <legend id='EftpL'><style id='EftpL'><dir id='EftpL'><q id='EftpL'></q></dir></style></legend>