Use mechanize to submit form without control name

假如想象 提交于 2019-11-28 00:25:52

So, I'll grant you, this one was pretty tough to figure out. Had to literally take a look at the mechanize code itself to figure out. Unfortunately, I couldn't test it for sure on an actual form item without a name attribute, though I can do so if you provide the site you're trying to pull, or you can do it yourself.

The forms objects honestly aren't implemented so well for usability. The only way I can see for you edit a nameless form control's value is by using the form's 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 you'd do here to set the control you're looking for is use the nr argument to grab it, using the index of the control in the form. Unfortunately, you can't use negative integers to grab controls from the back, so to grab the last form you'd have to do something along the lines of nr=len(myform.controls)-1.

In any case, what you can then do here is use that set_value method, and you should be set, as such:

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 it goes.

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