Use mechanize to submit a form without a control name

I am trying to use mechanize for python to submit a form, but the form control that I need to fill out does not have a name assigned to it.

<POST https://sample.com/anExample multipart/form-data <HiddenControl(post_authenticity_token=) (readonly)> <HiddenControl(iframe_callback=) (readonly)> <TextareaControl(<None>=)>> 

The control I'm trying to change is the last control in the above object, <TextareaControl(<None>=)> .

I looked through the documentation and cannot find a way to assign a value to this control since it does not have a name associated with it.

  forms = [f for f in br.forms()] print forms[2].controls[5].name 

Outputs No

+4
source share
1 answer

So, I will give you, it was quite difficult to understand. To take a literal look at mechanize code to understand. Unfortunately, I could not check it on the actual form element without the name attribute, although I can do this if you provide the site that you are trying to pull out, or you can do it yourself.

Form objects are honestly not as well implemented for ease of use. The only way you can edit the control value of an anonymous form is to use the set_value method:

 class HTMLForm: # <...> set_value(value, name=None, type=None, kind=None, id=None, nr=None, by_label=False, # by_label is deprecated label=None) 

So, what will you do here to set the control you are looking for, use the nr argument to capture it using the index of the control on the form. Unfortunately, you cannot use negative integers to grab controls from the back, so to grab the last form, you need to do something on the lines nr=len(myform.controls)-1 .

In any case, what you can do here is to use the set_value method, and you should be set as follows:

 forms[2].set_value("LOOK!!!! I SET THE VALUE OF THIS UNNAMED CONTROL!", nr=5) 

and that should work for you. Let me know how this happens.

+6
source

All Articles