2016-01-13 27 views
0

我剛剛寫了一個簡單的網頁抓圖腳本,以便爲我提供特定網站頁面上的所有插曲鏈接。該劇本運行良好,但現在已經破產。我沒有改變任何東西。在緩存中找不到元素 - Selenium(Python)

試試這個URL(拆解): - http://www.crunchyroll.com/tabi-machi-late-show

現在,腳本工作中途,給我的錯誤,指出,「元素在緩存中沒有發現 - 也許是頁面已經因爲它是改變擡頭看'

我在網上查了一下,有人說在某些地方使用'隱式等待'命令。我做到了,仍然沒有運氣。

更新:我在降級桌面上試過這個腳本,它在那裏工作沒有任何問題。

這裏是我的腳本: -

from selenium import webdriver 
from selenium.webdriver.common.keys import Keys 
import os 
import time 
from subprocess import Popen 

#------------------------------------------------ 

try: 
    Link = raw_input("Please enter your Link : ") 
    if not Link: 
     raise ValueError('Please Enter A Link To The Anime Page. This Application Will now Exit in 5 Seconds.') 
except ValueError as e: 
    print(e) 
    time.sleep(5) 
    exit() 

print 'Analyzing the Page. Hold on a minute.' 
driver = webdriver.Firefox() 
driver.get(Link) 

assert "Crunchyroll" in driver.title 
driver.implicitly_wait(5) # <-- I tried removing this lines as well. No luck. 
elem = driver.find_elements_by_xpath("//*[@href]") 
driver.implicitly_wait(10) # <-- I tried removing this lines as well. No luck. 
text_file = open("BatchLink.txt", "w") 
print 'Fetching The Links, please wait.' 
for elem in elem: 
    x = elem.get_attribute("href") 
    #print x 
    text_file.write(x+'\n')   


print 'Links have been fetched. Just doing the final cleaning now.' 
text_file.close() 

CleanFile = open("queue.txt", "w") 
with open('BatchLink.txt') as f: 
    mylist = f.read().splitlines() 
    #print mylist 
    with open('BatchLink.txt', 'r') as inF: 
    for line in inF: 
     if 'episode' in line: 
      CleanFile.write(line) 

print 'Please Check the file named queue.txt' 
CleanFile.close() 
os.remove('BatchLink.txt') 
driver.close() 

下面是錯誤的截圖(可能會有所幫助): http://i.imgur.com/SaANlsg.png

回答

1

好吧,我不使用Python工作,但知道的問題

你有變量,你init - >elem = driver.find_elements_by_xpath("//*[@href]")

之後,你在循環0中做一些事情你以前完成環嘗試初始化這個變量再次

elem = driver.find_elements_by_xpath("//*[@href]") 

的事情是,在DOM變化和你失去的元素集合。

+0

好吧,我試着在循環結尾添加它,仍然是同樣的錯誤。 – Xonshiz