user14585061
user14585061

Reputation:

Hi I am experimenting with selenium in python

Hi I am experimenting with selenium in python, I basically want to load this website https://www.jdsports.co.uk/c/footwear/?max=72 and get each product with a name and price. I am also using tkinter GUI if any of that confuses you, so for example I run it, It loads up the main menu I click the section I want to look at, it opens the browser, now it loads in and then I run into this error each time

Exception in Tkinter callback
Traceback (most recent call last):
  File "E:\Python\lib\tkinter\__init__.py", line 1883, in __call__
    return self.func(*args)
  File "C:\Users\James\Desktop\WebScraper\webscraper.py", line 34, in <lambda>
    scrapeBtn = tkinter.Button(secndmenu, text='scrape', command=lambda url=url:scrape(url,secndmenu))
  File "C:\Users\James\Desktop\WebScraper\webscraper.py", line 49, in scrape
    print(products.get_attribute('text'))
AttributeError: 'list' object has no attribute 'get_attribute'

Here is the code

#imports
from selenium import webdriver
import tkinter

#Window Calls
home = tkinter.Tk()
home.title("Home")
home.geometry('500x500')

urlselec=''

def shopClothing():
    urlselec="https://www.jdsports.co.uk/c/clothing/?max=72"
    url = urlselec
    return url, scrapeOrAuto(url)

def shopFootWare():
    urlselec='https://www.jdsports.co.uk/c/footwear/?max=72'
    url = urlselec
    return url, scrapeOrAuto(url)

def shopAccessories():
    urlselec='https://www.jdsports.co.uk/c/accessories/'
    url = urlselec
    return  url, scrapeOrAuto(url)


def scrapeOrAuto(url):
    home.withdraw()
    secndmenu = tkinter.Tk()
    secndmenu.title('Choose')
    secndmenu.geometry('300x300')
    url = url
    scrapeBtn = tkinter.Button(secndmenu, text='scrape', command=lambda url=url:scrape(url,secndmenu))

    scrapeBtn.pack()
    secndmenu.mainloop()

def scrape(url,secndmenu):
    secndmenu.withdraw()
    scraperAlert = tkinter.Tk()
    scraperAlert.title('Alert!')
    scraperAlert.geometry('300x200')

    tkinter.Label(scraperAlert,text='Scraping!').pack()
    browser = webdriver.Chrome()
    browser.get(url)
    products = browser.find_elements_by_class_name("productListItem ")
    print(products.get_attribute('text'))
    # soruce = browser.page_source
    print(products)

    return print('scraped')

#Buttons
clothing = tkinter.Button(anchor='center',text="Clothing", command =shopClothing)
footware = tkinter.Button(anchor='center',text='Footware', command=shopFootWare)
accessories = tkinter.Button(text='Accessories', command=shopAccessories)



#WindowCompile
clothing.pack()
footware.pack()
accessories.pack()
home.mainloop()


Upvotes: 1

Views: 52

Answers (1)

cadolphs
cadolphs

Reputation: 9647

Just reading the error message, not knowing anything particular about TkInter or Selenium, tells me that products is a list, and in Python you can't call get_attribute on a list.

I assume that products is a list of browser elements with class name productListItem and as such what you want is to get your hands on the elements of that list and then call get_attribute on those!

So maybe

for product in products:
  print(product.get_attribute('text'))

might work.

Upvotes: 1

Related Questions