TypeError: #39;WebElement#39; object is not subscriptable(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- replaywill 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"对象不可下标的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:TypeError:“WebElement"对象不可下标
 
				
         
 
            
        基础教程推荐
- 在同一图形上绘制Bokeh的烛台和音量条 2022-01-01
- 包装空间模型 2022-01-01
- 求两个直方图的卷积 2022-01-01
- 在Python中从Azure BLOB存储中读取文件 2022-01-01
- PANDA VALUE_COUNTS包含GROUP BY之前的所有值 2022-01-01
- Plotly:如何设置绘图图形的样式,使其不显示缺失日期的间隙? 2022-01-01
- 无法导入 Pytorch [WinError 126] 找不到指定的模块 2022-01-01
- 使用大型矩阵时禁止 Pycharm 输出中的自动换行符 2022-01-01
- PermissionError: pip 从 8.1.1 升级到 8.1.2 2022-01-01
- 修改列表中的数据帧不起作用 2022-01-01
 
    	 
    	 
    	 
    	 
    	 
    	 
    	 
    	 
				 
				 
				 
				