TypeError:“WebElement"对象不可下标 [英] TypeError: 'WebElement' object is not subscriptable

查看:39
本文介绍了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]


参考

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

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:

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

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆