vendredi 25 septembre 2020

list index out of range an if statement that's in a for loop

I am web scraping with selenium for articles but there are some ads there from the site which don't contain text. So I cant scrape the text because it doesn't have any. So I thought I could use an if statement but it still gives the error. How can I fix this?

heres the code:

for i in range(10):
    dict1 = {}
    driver.get("https://www.vrt.be/vrtnws/nl/rubrieken/economie/")
    sleep(1)

    articles = driver.find_elements_by_class_name("vrt-teaser__title-text")
    a = articles[i].text
    articles[i].click()
    sleep(1)
    if driver.find_elements_by_class_name("cmp-text")[0].text and driver.find_elements_by_class_name("cmp-text")[1].text:
        tupletext = driver.find_elements_by_class_name("cmp-text")[0].text, driver.find_elements_by_class_name("cmp-text")[1].text
    else:
        a = "None"
        tupletext = "None"
    dict1 = a, tupletext
    csv_writer.writerow([a,tupletext])
    print(dict1)
    sleep(1)

Aucun commentaire:

Enregistrer un commentaire