Home > Net >  Cannot Iterate Web element('list' object has no attribute 'text')
Cannot Iterate Web element('list' object has no attribute 'text')

Time:02-24

I am getting below error when I try to iterate all the products from a webpage, can anyone please advise what am i doing wrong?

Error : AttributeError: 'list' object has no attribute 'text'

Below is my code:

from selenium import webdriver

from selenium.webdriver.common.by import By

from selenium.webdriver.support.ui import WebDriverWait

from selenium.webdriver.support import expected_conditions as EC

from selenium.webdriver.common.keys import Keys

import pandas as pd
from bs4 import BeautifulSoup
import requests as r


import time

from selenium.webdriver.support.ui import Select

PATH="chromedriver.exe"


driver=webdriver.Chrome(PATH)

url1="https://valuebuds.com/pages/search-results-page"

driver.get(url1)

Select(WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "select#year_field")))).select_by_visible_text('1999')

driver.find_element_by_class_name("agree").click()

print("***CODE IS WORKING***")

products=driver.find_elements_by_id("snize-search-results-grid-mode")


for i in products:

   title=i.find_elements_by_class_name("snize-title").text

   print(title)

    
driver.quit()

Any help will be appreciated. Thanks

CodePudding user response:

Instead of this:

products=driver.find_elements_by_id("snize-search-results-grid-mode")

for i in products:

title=i.find_elements_by_class_name("snize-title").text

print(title)

Try this:

products = driver.find_elements_by_class_name("snize-title")
print(len(products))
title = [prod.text for prod in products]  # I kept in a list, but it's your choice, you can directly print the title for each with below lines:
# for title in products:
# print(title.text)
print(title)

there are 15 items with snize-title and are directly accessible.

Here is the output:

CODE IS WORKING
15
['HONEYSTICK BEEMASTER 510 BATTERY MULTI KIT', 'CLIPPER LIGHTER MICRO - RAW CLASSIC/BLACK', 'DABWARE PLATINUM DIAMOND SILICONE SPOON PIPE', 'DABWARE PLATINUM CLASSIC STYLE SILICONE SPOON PIPE', 'KARMA GLASS 9" SKINNY BEAKER GLASS BONG', "STONESMITHS' SLASH REPLACEMENT CHAMBER", 'MOOSE LABS MOUTHPEACE FILTER FULL KIT', 'PIECEMAKER KONJURER SILICONE SHERLOCK PIPE', 'KING PALM MINI PRE ROLL POUCH - 5 PACK', 'PURPLE POWER ULTRA INSTANT CLEANING FORMULA - 8OZ', 'BOLD MAC1 (H) DRIED - 3.5G', 'BOLD THE VISION (H) DRIED - 3.5G', 'CRAFTPORT PINK LEMONADE (IND) PRE-ROLL - 0.5G X 2', 'POOLBOY CHEM DAWG (IND) PRE-ROLL - 0.35G X 5', 'MARLEY NATURAL BLACK MASTER KUSH (IND) DRIED - 3.5G']

Process finished with exit code 0
  • Related