0

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

1
  • It simple means selenium did not find any element with that class, try to be more specific when finding HTML tags, maybe give id if possible. Commented Feb 13, 2022 at 2:51

1 Answer 1

1

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

Sign up to request clarification or add additional context in comments.

1 Comment

short and crisp answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.