Проблема с селеновым скребком, indexError - PullRequest
0 голосов
/ 03 марта 2020

это мой код, он используется для получения различных данных из онлайн-хранилища и представления их в формате csv

class Selenium:

#find_element_by_name('')
#find_element_by_xpath('')
#find_elements_by_class_name('')
#find_element_by_id('')
# giacenza = webdriver.find_elements_by_css_selector('.tdLarghezzaArt > .clFascia .tdwithAut > .testoGiac')
# giacenza = webdriver.find_elements_by_class_name('testoGiacVal')
# giacenza2 = webdriver.find_elements_by_class_name('testoGiacVal')

def __init__(self, webdriver, x, y):
    with open('File-Results.csv', 'w') as f:
        f.write("Codice ;Prezzo; Giacenza \n")

        def testo_prezzo():
            prezzo = webdriver.find_elements_by_class_name('testoPrezzo')
            return prezzo

        def giacenza():
            giacenza = webdriver.find_elements_by_class_name('testoGiac')
            return giacenza

        def giacenza_2():
            giacenza2 = webdriver.find_elements_by_css_selector('.testoGiacVal')
            return giacenza2
        # self.giacenza = webdriver.find_elements_by_class_name('testoGiacVal')

        def codice_prodotto():
            codice = webdriver.find_elements_by_class_name('testo_codiceArt')
            return codice

        def for_def():
            # cnt = 0
            test = len(giacenza())
            with open('File-Results.csv', 'a') as f:
                for i in range(test):
                    if giacenza()[i].text == "Disponibile":
                        giac = giacenza()[i].text
                        print(1, codice_prodotto()[i].text + " " + giac)
                    else:
                        giac = giacenza_2()[i].text
                        print(2,codice_prodotto()[i].text + " " + giac)

                    f.write(
                        codice_prodotto()[i].text + ";" + testo_prezzo()[i].text + ";" + giac + "\n")

    self.username = webdriver.find_element_by_name("username").send_keys(x)
    self.password = webdriver.find_element_by_name("password").send_keys(y)
    time.sleep(3)
    self.login = webdriver.find_element_by_xpath(
        '//*[contains(concat( " ", @class, " " ), concat( " ", "buttonLogin", " " ))]').click()
    time.sleep(5)
    self.cookie = webdriver.find_element_by_class_name("buttonCookie").click()
    time.sleep(5)

    self.SCROLL_PAUSE_TIME = 20

    self.last_height = webdriver.execute_script("return document.body.scrollHeight")

    while True:

        webdriver.execute_script("window.scrollTo(0, document.body.scrollHeight);")

        time.sleep(self.SCROLL_PAUSE_TIME)

        self.new_height = webdriver.execute_script("return document.body.scrollHeight")
        if self.new_height == self.last_height:
            break
        self.last_height = self.new_height

        for_def()

    time.sleep(5)
    webdriver.close()

    self.end_msg = messagebox.showinfo(title="\t", message="Grazie per aver utilizzato ReadyDrop")

в конце цикла, код должен быть «неопределяемым» для продуктов без 'giacenza' и инвентарь для тех, у кого 'giacenza'

вместо продолжения происходит сбой программы, возвращая эту ошибку:

gia c = giacenza_2 () [i] .text IndexError : индекс списка вне диапазона

1 Ответ

0 голосов
/ 03 марта 2020

giacenza() и giacenza_2(), похоже, содержат разное количество элементов, порой приводящих к ошибке. Добавление проверки для giacenza_2 может помочь избежать ошибки:

class Selenium:

#find_element_by_name('')
#find_element_by_xpath('')
#find_elements_by_class_name('')
#find_element_by_id('')
# giacenza = webdriver.find_elements_by_css_selector('.tdLarghezzaArt > .clFascia .tdwithAut > .testoGiac')
# giacenza = webdriver.find_elements_by_class_name('testoGiacVal')
# giacenza2 = webdriver.find_elements_by_class_name('testoGiacVal')

 def __init__(self):
    with open('./File-Results.csv', 'w') as f:
        f.write("Codice ;Prezzo; Giacenza \n")

        def testo_prezzo():
            prezzo = webdriver.find_elements_by_class_name('testoPrezzo')
            return prezzo

        def giacenza():
            giacenza = webdriver.find_elements_by_class_name('testoGiac')
            return giacenza

        def giacenza_2():
            giacenza2 = webdriver.find_elements_by_css_selector('.testoGiacVal')
            return giacenza2
        # self.giacenza = webdriver.find_elements_by_class_name('testoGiacVal')

        def codice_prodotto():
            codice = webdriver.find_elements_by_class_name('testo_codiceArt')
            return codice

        def for_def():
            # cnt = 0
            test = len(giacenza())
            with open('File-Results.csv', 'a') as f:
                for i in range(test):
                    if len(giacenza())<=i and giacenza()[i].text == "Disponibile":
                        giac = giacenza()[i].text
                        print(1, codice_prodotto()[i].text + " " + giac)
                    else:
                            if giacenza_2()<=i:
                                    giac = giacenza_2()[i].text
                                    print(2,codice_prodotto()[i].text + " " + giac)
                                    f.write(codice_prodotto()[i].text + ";" + testo_prezzo()[i].text + ";" + giac + "\n")

    self.username = webdriver.find_element_by_name("username").send_keys(x)
    self.password = webdriver.find_element_by_name("password").send_keys(y)
    time.sleep(3)
    self.login = webdriver.find_element_by_xpath(
        '//*[contains(concat( " ", @class, " " ), concat( " ", "buttonLogin", " " ))]').click()
    time.sleep(5)
    self.cookie = webdriver.find_element_by_class_name("buttonCookie").click()
    time.sleep(5)

    self.SCROLL_PAUSE_TIME = 20

    self.last_height = webdriver.execute_script("return document.body.scrollHeight")

    while True:

        webdriver.execute_script("window.scrollTo(0, document.body.scrollHeight);")

        time.sleep(self.SCROLL_PAUSE_TIME)

        self.new_height = webdriver.execute_script("return document.body.scrollHeight")
        if self.new_height == self.last_height:
            break
        self.last_height = self.new_height

        for_def()

    time.sleep(5)
    webdriver.close()

    self.end_msg = messagebox.showinfo(title="\t", message="Grazie per aver utilizzato ReadyDrop")
ob = Selenium()
ob.for_def()
...