我正在尝试从网站获取剧集链接。 所以我需要输入链接并从剧集中获取信息。 它在 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)
当元素未附加到 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();