TypeError:“WebElement”对象不是subscriptab

2024-10-03 17:24:31 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图用Python在spotifywebplayer上按下重放按钮,但是得到了这个错误。如何在网络播放器中按下按钮?在

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] TypeError: 'WebElement' object is not subscriptable

谢谢:)


Tags: divreplaybyhtmldriver错误bodybutton
2条回答
find_element_by_xpath

返回第一个找到的元素(不是数组)

^{pr2}$

或者

find_elements_by_xpath(...)[0].click()

正如@KunduK注释的那样,删除[0]。在

您使用的是绝对xPath,不建议这样做。在

尝试使用相对xPath

如果有几个按钮,并且需要首先在xpath中使用[0],如下所示:

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() 

相关问题 更多 >