2014-12-22 23 views
3

如寫在題目我不得不改變使用機械化一些輸入字段的值,但我沒有,只ID的名稱:/讓我們堅持這一點。Python的機械化更改不願透露姓名的輸入值(稱爲ID)

這是形式的外觀:

<form id="Login" name="Login"> 

    <div id="login-inputs-div"> 
     <input id="Username" type="hidden" name="username"></input> 
     <input id="Password" type="hidden" name="password"></input> 
     <input id="PopupUsername" class="input-text input-text-gray" type="text" disabled="disabled" value="admin" maxlength="32" style="width:100px;"></input> 
     <input id="PopupPassword" class="input-text input-text-gray " type="password" maxlength="32" value="" style="width:100px;" placeholder="hasło"></input> 
     <input id="bt_authenticate" class="input-btn input-btn-orange translation Translations.general.btn.authenticate translated" type="submit" value="zaloguj"></input> 
    </div> 
    <div id="logout-link-div" style="display: none;"></div> 

</form> 

我必須做些什麼?使用某個值填充PopupPassword並稍後提交?

我的做法是這樣的:

import mechanize 

url = "http://192.168.1.1" 
br = mechanize.Browser() 
br.set_handle_robots(False) 
br.open(url) 

br.select_form(name="Login") 
br.form().find_control(id="PopupPassword").__setattr__("value", "something") 
res = br.submit() 
content = res.read() 

林期待着一些解決方案,在此先感謝。

+0

使用'find_control()'正是這裏需要做的。問題是什麼?謝謝。 – alecxe

+0

那麼不知何故,返回AttributeError的:HtmlForm控件實例沒有__call__方法,我不知道如何改變屬性:■ – windoo

回答

5

find_control()正是你需要在這裏。只是不要撥打br.form,它不是一個功能:

password_field = br.form.find_control(id="PopupPassword") 
password_field.value = "something" 
+0

似乎工作感謝您的幫助:) – windoo

+0

嗯,反正那LIVEBOX配置網頁是有點遲鈍,或IM困了,但我無法登錄到它,仍然謝謝。 – windoo

+0

@windoo可能是別的東西。考慮用細節創建一個單獨的問題。謝謝。 – alecxe