I'm making a scraper to scrape a betting website for its data, this is an example piece of code that will scrape the event, teams and odds. The idea is to iterate through all the competitions that are available for the sport. I am able to locate the links and click on the first one, then I am also able to scrape the data and return it in a list (to later be put into a database). My issue is I am not able to return to the previous page to click the next link, I get an error when trying to do this. Here is my code:
driver = webdriver.Chrome(Path)
driver.get("https://www.neds.com.au/sports/table-tennis/")
a = []
links = driver.find_elements_by_class_name("matches-filter__link")
for l in links:
l.click()
tt_matches = driver.find_elements_by_class_name("sport-event-card")
for match in tt_matches:
Match = match.find_element_by_css_selector(".sports-event-title__name-text").text
a.append(Match)
Teams = match.find_elements_by_css_selector(".price-button")
for team in Teams:
team_name = team.find_element_by_css_selector(".price-button-name").text
team_odd = team.find_element_by_css_selector(".price-button-odds-price span").text
a.append(team_name)
a.append(team_odd)
driver.back()
time.sleep(2)
driver.quit()
and it returns this error:
raise exception_class(message, screen, stacktrace)
StaleElementReferenceException: stale element reference: element is not attached to the page document (Session info: chrome=84.0.4147.105)
How can I fix this, I think the issue is with the driver.back(), I have also tried l.back() and it doesn't work still.