Novita Anggraini
Novita Anggraini

Reputation: 1

IndexError: list index out of range (Selenium)

i try to scrap link product in website gramedia bookstore, but I get problem like this

import selenium
import time
from selenium import webdriver
from webdriver_manager.chrome import ChromeDriverManager
    
    driver = webdriver.Chrome(ChromeDriverManager().install())
    driver.get('https://www.gramedia.com/categories/buku?based_on=best-seller')
    listOfLinks=[]
    
    for i in range(3):
        time.sleep(3)
        productInfoList = driver.find_elements_by_class_name('product-list')
        productInfoList[0].text
        for perProduct in productInfoList:
            classPerProduct = perProduct.find_elements_by_tag_name('div')[0]
            linkProduct = classPerProduct.find_element_by_tag_name('a')
            listOfLinks.append(linkProduct.get_property('href'))
        try:
            driver.find_elements_by_class_name('ion-ios-arrow-forward')[1].click()
        except:
            continue
    
    with open("data_link.txt", "w") as f:
        for s in listOfLinks:
            f.write(str(s) +"\n")

I get an error message like this:

c:\Users\Userz\latihan\scrap.py:6: DeprecationWarning: executable_path has been deprecated, please pass in a Service object
  driver = webdriver.Chrome(ChromeDriverManager().install())

DevTools listening on ws://127.0.0.1:58426/devtools/browser/a5f69f68-c236-4dd4-9bca-7912a57bb86d
c:\Users\Userz\latihan\scrap.py:12: DeprecationWarning: find_elements_by_* commands are deprecated. Please use find_elements() instead
  productInfoList = driver.find_elements_by_class_name('product-list')
Traceback (most recent call last):
  File "c:\Users\Userz\latihan\scrap.py", line 13, in <module>
    productInfoList[0].text
IndexError: list index out of range

Please help me to fix this code

Upvotes: 0

Views: 1169

Answers (1)

George Imerlishvili
George Imerlishvili

Reputation: 1957

product-list class doesn't exist hence productInfoList returns an empty list. that is why you get indexError when you are trying to get 0 index from productInfoList, Try more specific selectors

Upvotes: 1

Related Questions