【问题标题】:Splinter object has no attribute click分裂对象没有属性点击
【发布时间】:2014-06-26 17:55:36
【问题描述】:

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

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'

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

【问题讨论】:

    标签: python attributeerror splinter


    【解决方案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()
    

    【讨论】:

    • @Jon 然后找不到您要查找的元素。你可能有你的 xpath 错误。见ElementDoesNotExist exception
    • 是的,我想我的 xpath 中有错字,我重新复制并粘贴并添加了 .first,它现在可以工作了,谢谢!
    • (至少在较新的 /2018/ 版本的 splinter 中)应该将其从 ElementList 代理(参见 readthedocs)到其第一个对象。调用 ElementList.click() ; ElementList.first.click() ; ElementList[0].click() 应该是相同的。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-10-26
    • 2016-05-04
    • 2021-04-28
    • 1970-01-01
    • 2017-07-07
    • 1970-01-01
    相关资源
    最近更新 更多