Reputation: 2299
I want to log in to my online bank account and print the transaction history.
I'm using an alternative to mechanize called Splinter because it's much easier to use and more clearly documented.
The code I wrote gives me an error when trying to fill the password form. I can't seem to successfully identify the password form field. Since it doesn't have a "name=" attribute or a css class attribute.
Here's the code:
username2 = '***'
password2 = '***'
browser2 = Browser()
browser2.visit('https://mijn.ing.nl/internetbankieren/SesamLoginServlet')
browser2.find_by_css('.firstfield').fill(username2)
browser2.find_by_id('#ewyeszipl').fill(password2)
browser2.click_link_by_text('Inloggen')
url2 = browser2.url
title2 = browser2.title
titlecheck2 = 'Mijn ING Overzicht - Mijn ING'
print "Stap 2 (Mijn ING):"
if title2 == titlecheck2:
print('Succeeded')
print 'The source is:'
print browser2.html
browser2.quit()
else:
print('Failed')
browser2.quit()
The full traceback:
/Library/Frameworks/Python.framework/Versions/2.7/bin/python "/Users/*/Dropbox/Python/Test environment 2.7.3/Splinter.py"
Traceback (most recent call last):
File "/Users/*/Dropbox/Python/Test environment 2.7.3/Splinter.py", line 45, in <module>
browser2.find_by_id('#ewyeszipl').fill_form(password2)
File "/Users/*/Library/Python/2.7/lib/python/site-packages/splinter/element_list.py", line 73, in __getattr__
self.__class__.__name__, name))
AttributeError: 'ElementList' object has no attribute 'fill_form'
Process finished with exit code 1
Upvotes: 0
Views: 6073
Reputation: 2299
Problem solved with the help of Miklos.
Here's the working code:
from splinter import *
# 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
Upvotes: 2
Reputation: 4585
According to Splinter's documentation, you can chain the finding methods. I suggest you try this (I couldn't test it myself!) :
browser2.find_by_id('gebruikersnaam').first.find_by_tag('input').fill(username2)
browser2.find_by_id('wachtwoord').first.find_by_tag('input').fill(password2)
Notice I also changed the instruction to find the input field for the username, even if you could find it with the .firstfield
CSS class. I just think it looks a little clearer/cleaner when you select the containing div first and then look for the input field in there.
Upvotes: 1