编程语言
首页 > 编程语言> > Python Mechanize更改未命名的输入值(已知id)

Python Mechanize更改未命名的输入值(已知id)

作者:互联网

正如在主题中写的我必须使用mechanize更改某些输入字段的值,但我没有它的名称只有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()

我期待着一些解决方案,提前谢谢.

解决方法:

find_control()正是你在这里需要的.只是不要调用br.form,它不是一个函数:

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

标签:html,python,forms,mechanize
来源: https://codeday.me/bug/20190830/1766722.html