类型错误:“WebElement"对象不可下标 [英] TypeError: 'WebElement' object is not subscriptable
问题描述
我尝试使用 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]replay.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]类型错误: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""")
将始终返回一个单个 WebElement.因此你不能通过任何索引访问元素,例如[0]
、[1]
等作为索引只能与列表相关联.>
解决方案
有两种方法可以解决这个问题.
在第一种方法中,您可以删除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]
参考
您可以在以下位置找到一些相关讨论:
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 casereplay
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 usingfind_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:
这篇关于类型错误:“WebElement"对象不可下标的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!