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

标签 python html forms mechanize

正如主题中所写,我必须使用 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"

关于Python Mechanize 更改未命名的输入值(已知 id),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27595639/

相关文章:

python - 如何将数据框中图像的扩展名转换为大写

python - Nose 测试运行器找到除标签之外的所有测试

javascript - 如何从 Animate CC 2015 获取不使用 jquery 的 Canvas 元素

javascript - Jquery 脚本不适用于新元素

python - 如何在路径中发送带有两个参数的 DELETE 请求?

python - Matplotlib - 如何为具有对数刻度的线图设置颜色条

javascript - 将 JS 合并到 Html 页面中

html - 水平列表的响应式分隔符

javascript - MooTools DatePicker 库 : 'value' is empty if the form is submitted

javascript - 停止不必要的按钮表单提交html js