Selenium TypeError: 'WebElement' object is not subscriptable

Viewed 508

I tried to create a dictionary by appending the items of a previous iterable list, and I don´t know what is the problem:

for i in range(3):    
    try:
    #primero: encontrar el boton:
        boton=WebDriverWait(driver,6).until(
            EC.presence_of_element_located(By.XPATH,'//div[@class="entrys-con-banner last clearfix"]//a[@class="btn"]')
        )
        boton.click()
    
    #segundo:esperar a que cargue la informacion:
        WebDriverWait(driver,7).until(
            EC.presence_of_all_elements_located((By.XPATH,'//article[@itemtype="http://schema.org/Article"]'))
        )
    
        sleep(random.uniform(4.0,6.0))
        
    except:
        break
        print("Error")
        

ElEconomista=[]

articulos=driver.find_elements_by_xpath('//article[@itemtype="http://schema.org/Article"]')

for articulo in articulos:
    autor=articulo.find_element_by_xpath('.//p/a').text
    titulo=articulo.find_element_by_xpath('.//div[@class="entry-data"]/h3/a').text
    try:
        nota=articulo.find_element_by_xpath('.//div[@class="entry-data"]/p').text 
    except:
        nota="NA"
    
    #se crea un diccionario para cada item
    articulo_dict=dict()
    
    articulo_dict["autor"]=articulo["autor"]
    articulo_dict["titulo"]=articulo["titulo"]
    articulo_dict["nota"]=articulo["nota"]
    
    ElEconomista.append(articulo_dict)

 
    

The previous code was modified by taking the advices given here, (the reason why the error below is different to the code above), but it stills give me the same error, so I decided to put all the code

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-1-01b021486e73> in <module>
     57 
     58     ElEconomista.append({
---> 59         "autor":articulo["autor"],
     60         "titulo":articulo["titulo"],
     61         "nota":articulo["nota"]

TypeError: 'WebElement' object is not subscriptable
1 Answers

You need to create a list of dictionaries. So first you create a specific dictionary for each article and fill it with the proper info and then append that dictionary to your list ElEconomista

for articulo in articulos:
    autor=articulo.find_element_by_xpath('.//p/a').text
    titulo=articulo.find_element_by_xpath('.//div[@class="entry-data"]/h3/a').text
    try:
        nota=articulo.find_element_by_xpath('.//div[@class="entry-data"]/p').text 
    except:
        nota="NA"

    articulo_dict = dict()
    articulo_dict["autor"] = autor
    articulo_dict["titulo"] = titulo
    articulo_dict["nota"] = nota

    ElEconomista.append(articulo_dict)
Related