2013-06-24 22 views
1

我想填寫兩個表格並登錄到我的銀行網站。用分號填寫密碼錶

我可以得到用戶名填寫的第一個表單,但我似乎無法獲取填寫密碼的表單。

下面是我使用的代碼:

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) 

我如何獲得密碼的形式來填補?

回答

8

這裏是工作代碼:

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 
+8

不要特別喜歡*進口...它使得它不清楚名稱來自哪裏。 '瀏覽器'是來自'selenium'還是來自'splinter'?如果它來自'分裂',爲什麼要'硒'進口呢? 「顯式比隱式更好。」 - 由蒂姆彼得斯的Python的禪宗 –

0

沒有得到太多深入到你的代碼,並採取快速瀏覽一下該網站(其中ID的都變了,我可以補充),我會說你可能在你的find_by_id('#easnhbcc')調用中增加了一個額外的'#'。 '#'是ID的典型CSS語言,但find_by_id()分裂調用並不期望它。 要麼

find_by_id('easnhbcc') 

find_by_css('#easnhbcc') 

可能會做的伎倆爲您服務。

1

該錯誤表明您正在嘗試填充元素列表。您只需選擇列表中的一個元素。你可能想要像這樣的東西:

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