Checkbox input using python mechanize

倖福魔咒の 提交于 2020-01-21 01:49:18

问题


I want to fill a form using python mechanize. form looks like:

<POST https://10.20.254.39/cloud_computing/vmuser/migrate_vm/cli multipart/form-data
  <TextControl(vm=cli)>
  <TextControl(chost=10.20.14.39)>
  <SelectControl(dhost=[*, 28, 27])>
  <CheckboxControl(live=[on])>
  <CheckboxControl(undefinesource=[on])>
  <CheckboxControl(suspend=[on])>
  <SubmitControl(<None>=Submit) (readonly)>
  <HiddenControl(_formkey=85819e5a-02bb-42c8-891f-3ddac485438b) (readonly)>
  <HiddenControl(_formname=migrate_create) (readonly)>>

How do i set the value of live or undefinesource (checkbox) to True(ticked) or False(untick) Items of live and undefinsource are:

>>> print br.form.controls[4].get_items()
[<Item name='on' id='migrate_undefinesource' checked='checked' name='undefinesource' type='checkbox' id='migrate_undefinesource' value='on' class='boolean'>]
>>> print br.form.controls[3].get_items()
[<Item name='on' id='migrate_live' checked='checked' name='live' type='checkbox' id='migrate_live' value='on' class='boolean'>]

回答1:


One way that I have done it is

br.find_control("live").items[0].selected=True


来源:https://stackoverflow.com/questions/6833868/checkbox-input-using-python-mechanize

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