我想下载2015到2019赛季所有滑冰运动员的游戏日志CSV文件:https://evolving-hockey.com/
但是,在for循环中会在不同的时间弹出一条错误消息。StaleElementReferenceException: Message: stale element reference: element is not attached to the page document
我看了看这个主题,我发现这是因为当网页在循环过程中刷新时,元素不再处于末日或改变……但在我的情况下,我没有找到任何可以纠正它的东西。我尝试添加一些time.sleep
,但仍然收到错误。下面是我的代码:
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()
https://stackoverflow.com/questions/55112063
复制相似问题