Python Mechanize 选择表单 FormNotFoundError
我想选择带有机械化的形式。这是我的代码:
br = mechanize.Browser()
self.br.open(url)
br.select_form(name="login_form")
表单的代码:
<form id="login_form" onsubmit="return Index.login_submit();" method="post" action="index.php?action=login&server_list=1">
但我收到此错误:
mechanize._mechanize.FormNotFoundError: no form matching name 'login_form
I want to select a form with mechanize. This is my code:
br = mechanize.Browser()
self.br.open(url)
br.select_form(name="login_form")
The form's code:
<form id="login_form" onsubmit="return Index.login_submit();" method="post" action="index.php?action=login&server_list=1">
But I'm getting this Error:
mechanize._mechanize.FormNotFoundError: no form matching name 'login_form
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
问题是您的表单没有名称,只有 id,并且它是
login_form
。您可以使用谓词:(如果
f.attrs
具有键id
,并且如果是,则id
值等于login_form
)。或者,如果您知道它是第一个、第二个等,则可以传递页面中表单的编号。例如,下面的行选择第一个表单:The problem is that your form does not have a name, only an id, and it is
login_form
. You can use a predicate:(where you se if
f.attrs
has the keyid
and, if so, theid
value is equal tologin_form
). Alternatively, you can pass the number of the form in the page, if you know if it is the first one, the second one etc. For example, the line below selects the first form:更具可读性:
然后:
a little more readable:
then: