Splinter对象没有click属性
我正在尝试从教育部下载一个文件,这是我目前写的完整代码:
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'
我之前也“点击”过其他类似的链接,所以这次我不太确定问题出在哪里。有没有人知道我为什么会遇到这个错误,以及有没有解决办法?
1 个回答
1
根据错误信息来看,从 br.find_by_xpath
返回的结果是一个列表,而不是单个元素。splinter 文档
也确认了这一点:
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()