When parsing a multi-page site, it gives an error: StaleElementReferenceException, how to be?
from selenium import webdriver from selenium.webdriver.common.keys import Keys from selenium.webdriver.support.ui import WebDriverWait from selenium.common.exceptions import StaleElementReferenceException import time print("ENTER MAIL: ") mail = input() print("ENTER PASSWORD: ") pas = input() driver= webdriver.Firefox() driver.get('https://www.udemy.com') print("GOT URL\n") time.sleep(5) driver.find_element_by_xpath("//button[@data-purpose='header-login']").click() print("OPEN LOGIN FORM\n") time.sleep(5) webdriver.ActionChains(driver).move_by_offset(570, 295).click().send_keys(mail).perform() time.sleep(5) print("PRINT MAIL\n") webdriver.ActionChains(driver).move_by_offset(100, 65).click().send_keys(pas).perform() time.sleep(5) print("PRINT PASSWORD\n") webdriver.ActionChains(driver).move_by_offset(0, 60).click().perform() time.sleep(5) print("AUTORIZATION\n") driver.find_element_by_xpath("//a[@data-purpose='my-courses']").click() time.sleep(6) print("GO TO URL\n") #strongs = driver.find_elements_by_xpath("//strong[@class = 'details__name']") f = open('udemy_titles','w', encoding='utf8') f.write("USERNAME: ") f.write(mail + "\n") f.write("PASSWORD: ") f.write(pas + "\n") f.write("TOTAL COURSES: ") try: div = driver.find_element_by_xpath("//div[@class='pager-label']").text div_1 = div.split(" ")[-2] f.write(div_1 + "\n") hrf = driver.find_element_by_xpath("//ul[@class='pagination pagination-expanded']/li[7]").text y = 0 while int(hrf) > y: # strongs = driver.find_elements_by_xpath("//strong[@class = 'details__name']") # time.sleep(1) driver.implicitly_wait(10) strongs = driver.find_elements_by_xpath("//strong[@class = 'details__name']") for strong in strongs: print(strong.text) y += 1 f.write("COURSE " + "#") f.write(str(y) + ": ") f.write(strong.text + "\n") driver.find_element_by_xpath("//span[@class = 'pagination-next udi udi-next']").click() time.sleep(5) #driver.get('https://www.udemy.com/home/my-courses/learning/?p=8') except: print("ONE PAGE\n") selem = 0 for strong in strongs: print(strong.text) selem += 1 f.write("COURSE " + "#") f.write(str(selem) + ": ") f.write(strong.text + "\n") f.close() driver.close() time.sleep(2) The script works up to 8 pages, further knocks out an error, there are no any more ideas.
