我想下载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()
发布于 2019-03-12 08:13:08
当元素不再是DOM的一部分或被刷新时,就会发生StaleElement异常。
对于您的情况,一种解决方案是实现一个可以重试的方法
from selenium.common.exceptions import StaleElementReferenceException
def click_element(driver, locator, value):
try:
driver.find_element(locator, value).click()
exception StaleElementReferenceException:
driver.find_element(locator, value).click()
在您的代码中,现在可以调用上面的click_element方法:
click_element(driver, "xpath", "//*[@id='tab-7262-1']/div/div[1]/div[1]/div/div/div/div[1]")
这应该适用于您的情况,因为从您的代码中,您只需单击。但是如果你需要执行更多的动作(比如send_keys,get_attribute(),text),你可以考虑在每个selenium调用周围实现一个包装器,并实现这种try/catch重试机制。
https://stackoverflow.com/questions/55112063
复制相似问题