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('//*[id@"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('//*[id@"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'

我之前已经“点击”过类似的其他链接,所以我不确定这次是什么问题。有谁知道我为什么会收到此错误以及是否有解决方法?

4

1 回答 1

1

根据错误消息,看起来返回的东西br.find_by_xpath是一个列表,而不是单个元素。splinter docs确认这一点:

Splinter 提供了 6 种在页面中查找元素的方法,每种选择器类型一种:css、xpath、tag、name、id、value。...这些方法中的每一个都返回一个包含找到的元素的列表。

它还说:

您可以使用第一个快捷方式获取第一个找到的元素:
first_found = browser.find_by_name('name').first

尝试像这样单击第一个元素:

br.find_by_xpath('//*[id@"ct100_ct100_CIPContent_ContentPlaceHolder1_LinkButton_FINALCIPtoSOCcrosswalk"]').first.click()

或使用列表索引:

br.find_by_xpath('//*[id@"ct100_ct100_CIPContent_ContentPlaceHolder1_LinkButton_FINALCIPtoSOCcrosswalk"]')[0].click()
于 2014-06-26T18:01:53.243 回答