如何选择单选按钮并提交表单

时间:2013-01-10 14:51:18

标签: python mechanize

这是HTML:

<form method="post" action="./xxxxx" name="form1">
    <input type="hidden" name="requestKey" value="52032a76cf53340b07052fd143feddc4a9d8f18130c80ec86003ab56e0d91b36"/>
    <input type="hidden" name="option" value="txnlist">
    <input type="hidden" name="account" value='1336169999'>
    <input type="hidden" name="app" value="D">
    <input type="hidden" name="lockType" value="">

    <p><span class="header2">&nbsp;Statement options </span>
        <table summary="" width="100%" border="0" cellpadding="1" cellspacing="1">
            <tr>
                <td width="20"></td>
                <td width="20"><input type="radio"  name="optDateFilter" value="1"></td>
                <td>Transactions for the last 30 days</td>
                <td></td>
            </tr>
            <tr>
                <td width="20"></td>
                <td width="20"><input type="radio" name="optDateFilter"  value="2"></td>
                <td>Transactions for the last 90 days</td>
                <td></td>
            </tr>
            <tr>
                <td width="20">&nbsp;</td>
                <td width="20"><input type="radio" name="optDateFilter" checked='checked' value="3" style="vertical-align: middle"></td>
                <td>
                    <div>
                        <font style="vertical-align: middle;">Transactions from&nbsp;</font>
                        <script src='jquery/js/ui/min/jquery.ui.datepicker.min-1.8.10.js'></script>
                        <script type='text/javascript'>$(function() {$('#datepicker1').datepicker( { changeMonth : true, changeYear : true, dateFormat : 'dd/mm/yy', showOn: 'both', buttonImage: 'images/calendar.png', buttonImageOnly: true }); }); </script>
                        <input type='text' id='datepicker1' name='txtFromDate' size='11' maxlength='10' value='14/12/2012' onchange="checkFromDate(document.form1.txtFromDate.value);document.form1.optDateFilter[2].checked = true;">
                        <font style="vertical-align: middle;">to</font>
                        <script type='text/javascript'>$(function() {$('#datepicker2').datepicker( { changeMonth : true, changeYear : true, dateFormat : 'dd/mm/yy', showOn: 'both', buttonImage: 'images/calendar.png', buttonImageOnly: true }); }); </script>
                        <input type='text' id='datepicker2' name='txtToDate' size='11' maxlength='10' value='01/01/2013' onchange="checkToDate(document.form1.txtFromDate.value,document.form1.txtToDate.value);document.form1.optDateFilter[2].checked = true;">
                    </div>
                </td>
                <td><font style="font-size: 8pt; vertical-align: middle;"><i>(maximum date range 3 months)</i></font></td>
            </tr>
        </table>
        <table summary="" cellpadding="1" cellspacing="1" border="0">
            <tr>
                <td width="20">&nbsp;</td>
                <td width="20">
                    <a  href="javascript:if(document.form1.optDateFilter[2].checked==true){CheckAndSubmit(document.form1.txtFromDate.value,document.form1.txtToDate.value);}else{showOverlay();document.form1.submit();};">
                         <img src="images/proceed.gif" alt="Show the selected range of transactions" border="0" width="22" height="22" />
                    </a>
                </td>
                <td>
                    <a  href="javascript:if(document.form1.optDateFilter[2].checked==true){CheckAndSubmit(document.form1.txtFromDate.value,document.form1.txtToDate.value);}else{showOverlay();document.form1.submit();};" title="Show the selected range of transactions">Show the selected range of transactions</a>
                </td>
            </tr>
    </table>
</form>

这是我的代码,但它不起作用:

self.browser.select_form('form1')
self.browser.form.set_all_readonly(False)
self.browser['txtFromDate'] = '28/12/12'
self.browser['txtToDate'] = '01/01/13'
resp = self.browser.submit()
html = resp.read()

2 个答案:

答案 0 :(得分:1)

我认为我必须这样做

    self.browser.select_form('form1')
    self.browser['optDateFilter'] = ['2'] 
    self.browser.form.find_control(name="txtFromDate", id="datepicker1").value = "13/10/12"
    self.browser.form.find_control(name="txtToDate", id="datepicker2").value = "01/01/13"
    resp = self.browser.submit()

答案 1 :(得分:1)

机械化有一个奇怪的怪癖:因为HTML文档中可能有多个单选按钮元素具有相同的名称,所以它坚持使用列表作为值,即使可以随时选择单个单选按钮。这意味着你需要使用它:

  self.browser.form.find_control(name='optDateFilter').value = ['2']

同样适用于更有意义的复选框,因为允许用户选择多个值。