I am trying to fetch episodes link from website. So i need to enter to the links and fetch the information from episodes for. Its working in 2-3 episodes then crashing with error that wrotted in the bottom. I tried to raise the time sleep but its still crashing.
Error :
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)