当前位置: 首页 > 知识库问答 >
问题:

消息:过时的元素引用:元素没有附加到页面文档PYTHON selenium

都才俊
2023-03-14

我正在尝试从网站获取剧集链接。所以我需要进入链接,从剧集中获取信息。它在2-3集中工作,然后在底部因错误而崩溃。我试着提高睡眠时间,但还是失败了。

错误:

selenium.common.exceptions.StaleElementReferenceException: Message: stale element reference: element is not attached to the page document
import time
import datetime
import random
import string
import json
import requests
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.chrome.options import Options
from urllib.request import urlopen
 
 
import urllib.request
def send_imdb(id):
    try:
        r = requests.get('http://sdarot.bnlstudio.com/import.php?id=%s' % (id))
        json = r.json()
        if json['status'] == "success":
            return json['id']
    except:
        return("Err")
 
links = []
seasons = [] 
link = "http://www.tvil.me/"

chrome_options = Options()
chrome_options.add_extension('gighmmpiobklfepjocnamgkkbiglidom.crx')

driver = webdriver.Chrome(chrome_options=chrome_options) 
driver.get(link)
#Getting Posts links and split them
page_right = driver.find_element_by_id("page-right")
posts = page_right.find_elements_by_xpath("//div[@class='index-episode-caption']/a")
for element in posts:
    href = element.get_attribute("href")
    splited = href.split("/")
    url = "%s//%s/view/%s/1/1/v/%s" % (splited[0], splited[2], splited[4], splited[8])
    links.append(url)
    ##print(url)
#Entering posts and gets IMDB ID
E = 0;
for link in links:
    driver.get(link)
    time.sleep(2)
    imdb_q = driver.find_element_by_xpath("//div[@id='view-trailer-imdb']/a")
    imdb = imdb_q.get_attribute("href")
    imdb_id = imdb.split("/")[4]
    post_id = send_imdb(imdb_id)
    print("Post_ID: %s" % (post_id))
    seasons_num = driver.find_elements_by_xpath("//*[contains(@id, 'change-season-')]/a")
    total_seasons_num = len(seasons_num)
 	
    for i in range(1, total_seasons_num):
        print("Season: %i" % (i))
        season = driver.find_element_by_css_selector("#change-season-{num} a".format(num=i))
        season.click()
        episodes = driver.find_elements_by_xpath("//*[contains(@id, 'change-episode-')]/a")
 
        for episode in episodes:
            E += 1
            print("Episode: %i" % (E))
            time.sleep(3)
            episode.click() # Break point
            time.sleep(3)

共有1个答案

白飞飙
2023-03-14

当元素未附加到DOM时,会发生陈旧元素异常。尝试在陈旧元素异常后再次查找元素。

考虑单击一个元素将刷新页面。
示例

 WebElement element = driver.findElement(By.Id("refreshButton"));
 element.Click(); //page will be refreshed after clicking this button
 element.Click(); //stale element exception will throw

现在,如果您对元素执行任何操作(单击、senkeys等),它将抛出StaleElementException,因为它的引用丢失(页面已刷新)

要克服这个问题,请在页面刷新后再次找到该元素。就像,

 WebElement element = driver.findElement(By.Id("refreshButton"));
 element.Click(); //page will be refreshed after clicking this button

 element = driver.findElement(By.Id("refreshButton"));
 element.Click();
 类似资料: