Search code examples
pythonattributeerrorsplinter

Splinter object has no attribute click


I'm trying to download a file from the Department of Education and this is my complete code as it stands so far:

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()

And this is the complete traceback that I get

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'

I've "clicked" on other links like this before so I'm not sure what the problem is this time. Does anyone know why I'm getting this error and if there is a way around it?


Solution

  • As per the error message it looks like the stuff returned from br.find_by_xpath is a list, not a single element. The splinter docs confirm this:

    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.

    It also says:

    You can get the first found element with the first shortcut:
    first_found = browser.find_by_name('name').first

    Try clicking the first element like this:

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

    Or using the list index:

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