Skip to main content
 首页 » 编程设计

python之split 对象没有属性点击

2025年05月04日10lidabo

我正在尝试从教育部下载一个文件,这是我目前为止的完整代码:

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' 

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

请您参考如下方法:

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

Splinter provides 6 methods for finding elements in the page, one for each selector type: css, xpath, tag, name, id, value. ... Each of these methods returns a list with the found elements.

它还说:

You can get the first found element with the first shortcut:
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()