Submitting Forms with Mechanize (Python)

痞子三分冷 提交于 2019-11-27 03:28:23

问题


Well, I am trying to login to a site using Python and mechanize.

I've got the site opened:

site = br.open("http://example.com/login.php")

And I've got a list of the forms (with br.forms).

<GET http://example.com/search.php application/x-www-form-urlencoded
<HiddenControl(search=1) (readonly)>
...
<POST http://example.com/login.php application/x-www-form-urlencoded
<TextControl(username=)>
<PasswordControl(password=)>
<CheckboxControl(stay=[1])>
<SubmitControl(<None>=Log in) (readonly)>>

I've been trying to submit the username and password fields.

I tried doing it like this:

br.select_form(nr=0)
br.form["username"] = 'usernamehere'
br.form["password"] = 'passwordhere'
br.submit()

Then I realised that the forms I were trying to fill in weren't the first on the page, but changing the 0 didn't help with anything. What should I do for selecting the form on a page like this?

However! That is not the only problem.

When I run it, I get this error:

Traceback (most recent call last):
File "C:\Python26\login.py", line 34, in <module>
br.form["username"] = 'usernamehere'
...
ControlNotFoundError: no control matching name 'username'

How can I fix this? D: Or am I doing it totally wrong? If it's the latter, how would I go about doing it?


回答1:


to select a form using its name you should use:

br.select_form(name="order")

what you are doing here:

br.form["username"] = 'usernamehere'

is trying to set a value to a control under the selected form, so when he can't find it, it throws the exception you are seeing.




回答2:


You might have several issues

  • if the form is generated through javascript, you can't solve it with mechanize - at least not in a straight forward way - in this case I recommend you to try and use selenium - you can try to look at the page HTML source - if you don't have the form there in pure html, it is pretty clear that it is inserted into DOM by javascript. Also, if the form is submitted through javascript, mechanize won't help you

  • also, the form might not be on the first page - you might want to set mechanize to follow the redirects



来源:https://stackoverflow.com/questions/8570920/submitting-forms-with-mechanize-python

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!