消息:陈旧元素引用:元素未附加到页面文档PYTHON selenium

时间:2018-05-02 12:11:53

标签: python selenium selenium-webdriver selenium-chromedriver

我正在尝试从网站上获取剧集链接。 所以我需要进入链接并从剧集中获取信息。 它在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 个答案:

答案 0 :(得分:0)

当元素未附加到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();