2014-06-26 128 views
2

我試圖下載從教育部文件,這是我的完整代碼,因爲它代表迄今:分裂對象沒有屬性,單擊

from splinter import Browser 
import time 

br = Browser() 
br.visit('http://nces.ed.gov/ipeds/cipcode/resources.aspx?y=55') 
br.find_by_xpath('//*[[email protected]"ct100_ct100_CIPContent_ContentPlaceHolder1_LinkButton_FINALCIPtoSOCcrosswalk"]').click() 

# give myself a delay to visually inspect that it's working 
time.sleep(5) 
br.quit() 

這是完整的回溯,我得到

File "crosswalksplinter.py", line 9, in <module> 
br.find_by_xpath('//*[[email protected]"ct100_ct100_CIPContent_ContentPlaceHolder1_LinkButton_FINALCIPtoSOCcrosswalk"]').click() 
File "/usr/lib/python2.6/site-packages/splinter/element_list.py", line 75, in __getattr__ 
self.__class__.__name__, name)) 
AttributeError: 'ElementList' object has no attribute 'click' 

我已經「點擊了」這樣的其他鏈接,所以我不確定這次是什麼問題。有誰知道我爲什麼會得到這個錯誤,如果有解決方法嗎?

回答

0

根據錯誤消息,它看起來像從br.find_by_xpath返回的東西是一個列表,而不是一個單一的元素。所述splinter docs確認:

分裂提供了6種方法用於查找頁面元素,一個用於每個選擇器類型:CSS,XPath中,標籤,姓名,ID,價值。 ...這些方法中的每一個都返回一個包含找到的元素的列表。

它還說:

你可以得到的第一個快捷找到的第一個元素:
first_found = browser.find_by_name('name').first

嘗試一下這樣的第一要素:

br.find_by_xpath('//*[[email protected]"ct100_ct100_CIPContent_ContentPlaceHolder1_LinkButton_FINALCIPtoSOCcrosswalk"]').first.click() 

或使用列表索引:

br.find_by_xpath('//*[[email protected]"ct100_ct100_CIPContent_ContentPlaceHolder1_LinkButton_FINALCIPtoSOCcrosswalk"]')[0].click() 
+0

@Jon然後找不到您正在查找的元素。你可能有你的xpath錯誤。參見['ElementDoesNotExist'例外](http://splinter.cobrateam.info/docs/finding.html#elementdoesnotexist-exception)。 – famousgarkin

+0

是的,我猜我在我的xpath中有一個錯字,我重複並粘貼並添加了.first,現在它可以工作,謝謝! – Jon

相關問題