TypeError: 'WebElement' object is not subscriptable

22,492

Solution 1

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:

Solution 2

find_element_by_xpath

returns first found element (not array)

find_element_by_xpath(...).click()

or

find_elements_by_xpath(...)[0].click()
Share:
22,492

Related videos on Youtube

Hi3t
Author by

Hi3t

Updated on January 11, 2022

Comments

  • Hi3t
    Hi3t over 2 years

    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
    
    • John Gordon
      John Gordon over 4 years
      find_element_by_xpath() returns a single item, not a list. Why are you using [0] on it?
    • KunduK
      KunduK over 4 years
      Remove [0] since you are using find_element_by_xpath()
    • Hi3t
      Hi3t over 4 years
      Lol thank you, it worked.
    • T-student
      T-student over 4 years
      it means that the ("""/html/body/div[2]/div/div[4]/div[3]/footer/div/div[2]/di‌​v/div[1]/div[5]/butt‌​on""") don't return a list ... probably you are pointing to the wrong element
  • Tea Tree
    Tea Tree almost 3 years
    Please elaborate a bit more. What is the difference between the different commands?
  • cach dies
    cach dies over 2 years
    one is plural return multiple elements the other is singular, return only one.