I want to download the game logs CSV file for all the skaters for the seasons 2015 to 2019 of: https://evolving-hockey.com/
However, there is an error message that pops up at different times in the for loop.
StaleElementReferenceException: Message: stale element reference: element is not attached to the page document
I look on the subject and what I found was that it is because when the webpage is refreshed during the loop the element is no longer in the DOOM or changed... But I did not find anything to correct it in my case. I try to add some time.sleep
, but I still get the error. Here is my code:
from selenium import webdriver
import csv
from selenium.webdriver.support.ui import Select
from datetime import date, timedelta
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
import time
from selenium.webdriver.common.keys import Keys
from selenium.common.exceptions import TimeoutException
chromedriver =("C:/Users/Michel/Desktop/python/package/chromedriver_win32/chromedriver.exe")
driver = webdriver.Chrome(chromedriver)
driver.get("https://evolving-hockey.com/")
#Click Games and then game logs
Gamestab= driver.find_element_by_xpath("/html/body/nav/div/ul/li[6]/a")
Gamestab.click()
Gameslog= driver.find_element_by_xpath("/html/body/nav/div/ul/li[6]/ul/li[3]/a")
Gameslog.click()
Strenght= driver.find_element_by_xpath("//*[@id='tab-7262-1']/div/div[1]/div[3]/div/div/button")
Strenght.click()
All=driver.find_element_by_xpath("//*[@id='tab-7262-1']/div/div[1]/div[3]/div/div/div/ul/li[1]/a")
All.click()
Totals=driver.find_element_by_xpath("//*[@id='game_logs_skaters_stat_display']/div[2]/div[1]")
Totals.click()
# Loop all teams and all seasons
# ## TEAM
for b in range(1,2340):
time.sleep(5)
Player= driver.find_element_by_xpath("//*[@id='tab-7262-1']/div/div[1]/div[1]/div/div/div/div[1]")
time.sleep(5)
Player.click()
Playername= driver.find_element_by_xpath("//*[@id='tab-7262-1']/div/div[1]/div[1]/div/div/div/div[2]/div/div[%d]" %(b))
time.sleep(5)
Playername.click()
# # ## Season- 20152016to20182019
for i in range(1,5):
Season=driver.find_element_by_xpath("//*[@id='tab-7262-1']/div/div[1]/div[2]/div/div/button")
time.sleep(5)
Season.click()
time.sleep(5)
Season1819=driver.find_element_by_xpath("//*[@id='tab-7262-1']/div/div[1]/div[2]/div/div/div/ul/li[%s]" %(i))
time.sleep(5)
Season1819.click()
## SUBMIT
submit = driver.find_element_by_id('game_logs_skaters_submit_button')
submit.click()
time.sleep(10)
# # Click download
download = driver.find_element_by_id('game_logs_skaters_download')
download.click()
driver.close()