Web抓取循环问题 - 未附加到页面文档的元素[重复]

问题描述 投票:0回答:1

我想为2015至2019年的所有滑冰选手下载游戏日志CSV文件:https://evolving-hockey.com/

但是,for循环中的不同时间会弹出一条错误消息。 StaleElementReferenceException: Message: stale element reference: element is not attached to the page document

我看一下这个主题,我发现是因为当在循环过程中刷新网页时,元素不再在DOOM中或者已经改变了......但是在我的情况下我找不到任何纠正它的东西。我尝试添加一些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()
python loops selenium web-scraping
1个回答
1
投票

当元素不再是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重试机制。

© www.soinside.com 2019 - 2024. All rights reserved.