我正在尝试使用python mechanize自动登录到网页但是收到错误。使用以下代码:
import mechanize
browser = mechanize.Browser()
browser.open('http://server:7180/cmf/home')
browser.select_form(nr=0)
browser.form['j_username'] = 'nathalok'
browser.form['j_password'] = '******'
browser.submit()
response = browser.open('http://server:7180/cmf/allHealthIssues')
print response.read()
但我得到以下错误:
Traceback (most recent call last):
File "mechanizetest.py", line 4, in <module>
browser.select_form(nr=0)
File "build/bdist.linux-x86_64/egg/mechanize/_mechanize.py", line 524, in select_form
mechanize._mechanize.FormNotFoundError: no form matching nr 0
我可以看到以下网页的源代码:
<form class="LoginForm well" action="/j_spring_security_check" method="POST">
<h1>Login</h1>
<label for="username"><i class="glyphicon tiny user"></i> Username:</label>
<input type="text" class="input-large" id="username" name="j_username" autofocus />
<label for="password"><i class="glyphicon tiny lock"></i> Password:</label>
<input type="password" class="input-large" id="password" name="j_password" autocomplete="off"/>
<input type="hidden" id="returnUrl" name="returnUrl" value=""/>
<label class="checkbox"><input type="checkbox" name="_spring_security_remember_me" /> Remember me on this computer.</label>
<hr/>
<button type="submit" class="btn btn-primary btn-large btn-block" name="submit">Login</button>
</form><!-- LoginForm -->
感谢有关如何自动登录或解决此错误的任何帮助。