【问题标题】:Filling a password form with Splinter使用 Splinter 填写密码表单
【发布时间】:2013-06-21 11:51:10
【问题描述】:

我正在尝试填写两张表格并登录我的银行网站。

我可以获得要填写的用户名的第一个表格,但我似乎无法获得要填写的密码表格。

这是我正在使用的代码:

from splinter import Browser

username2 = '***'
password2 = '***'

browser2 = Browser()
browser2.visit('http://mijn.ing.nl')

browser2.find_by_css('.firstfield').fill(username2)
browser2.find_by_id('#easnhbcc').fill(password2)

这是完整的追溯:

/usr/local/bin/python2 "/Users/narekaramjan/Dropbox/Python/Python 273/Test.py"
Traceback (most recent call last):
  File "/Users/narekaramjan/Dropbox/Python/Python 273/Test.py", line 26, in <module>
    browser2.find_by_id('#easnhbcc').fill(password2)
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/splinter/element_list.py", line 73, in __getattr__
    self.__class__.__name__, name))
AttributeError: 'ElementList' object has no attribute 'fill'

Process finished with exit code 1

我也试过了:

browser2.find_by_name('easnhbcc').fill(password2)

我怎样才能得到密码表来填写?

【问题讨论】:

    标签: python forms passwords webdriver splinter


    【解决方案1】:

    这是工作代码:

    from splinter import Browser     
    
    # Define the username and password
    username2 = '***'
    password2 = '***'
    
    # Choose the browser (default is Firefox)
    browser2 = Browser()
    
    # Fill in the url
    browser2.visit('https://mijn.ing.nl/internetbankieren/SesamLoginServlet')
    
    # Find the username form and fill it with the defined username
    browser2.find_by_id('gebruikersnaam').first.find_by_tag('input').fill(username2)
    
    # Find the password form and fill it with the defined password
    browser2.find_by_id('wachtwoord').first.find_by_tag('input').fill(password2)
    
    # Find the submit button and click
    browser2.find_by_css('.submit').first.click()
    
    # Print the current url
    print browser2.url
    
    # Print the current browser title
    print browser2.title
    
    # Print the current html source code
    print browser2.html
    

    【讨论】:

    • 不要特别喜欢 * 进口......这让人不清楚名称的来源。 Browser 是来自 selenium 还是来自 splinter?如果它来自splinter,为什么还要麻烦selenium 导入? “显式胜于隐式。” -- Python 之禅,蒂姆·彼得斯(Tim Peters)
    【解决方案2】:

    如果不深入了解您的代码,并快速查看该站点(我可能会添加 ID 已更改的位置),我会说您可能在 find_by_id('#easnhbcc' 中添加了一个额外的 '#' ) 称呼。 '#' 是 ID 的典型 CSS 语言,但 find_by_id() 分裂调用并不期望它。 要么

    find_by_id('easnhbcc')
    

    find_by_css('#easnhbcc')
    

    可能会为您解决问题。

    【讨论】:

      【解决方案3】:

      该错误表明您正在尝试填充元素列表。您只需选择列表中的元素之一。你可能想要这样的东西:

      find_by_name('foo').first.fill()
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2017-09-19
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多