在Mechanize中,如何设置ImageButton的x,y值?

时间:2013-08-12 06:54:00

标签: python mechanize imagebutton

我使用Mechanize Python作为Web客户端来提交表单。但是,我没有找到以这种形式设置ImageButton的x,y的方法。

我尝试过不同的方式:

1)第一个是找到控件:

bt = frm.find_control('ImageButtonID')
bt.x = 88
bt.y = 10

2)第二个是使用字典:

ctls = {}
ctls['ImageButton1.x'] = 88
ctls['ImageButton1.y'] = 10
data = urllib.urlencode(ctls)
br.open(url,data)

但是没有一种方法可行,有什么解决方案吗?非常感谢

这是html代码:

                          标题                                             函数EnterTo(){         if(window.event.keyCode == 13){         form1.submit();         }         }          函数ChangeImage()         {             document.getElementById(“yzm”)。src = document.getElementById(“yzm”)。src +'?';         }                            

<input name="deptID" type="hidden" id="deptID" value="1" /> <input name="dateType" type="hidden" id="dateType" value="Today" />
<input name="timeType" type="hidden" id="timeType" value="AM" />

<div class="numberBG">
<div class="Repeater">
    <table class="Item">

    <tr>
        <td>
                                    <input type="image" name="Repeater1$ctl00$ImageButton1" id="Repeater1_ctl00_ImageButton1" title="XXXXXXXX" src="images/1_1.jpg" style="border-width:0px;" />
        </td>
    </tr>
</table>        
<table class="table">
    <tr>
        <td style="width:100px;" class="txt">the code</td>
        <td style="width:90px;"><input name="Txt_Yzm" type="text" id="Txt_Yzm" onkeydown="if(event.keyCode==13)event.keyCode=9 " style="width:90px;" />
        <td style="width:50px;"><img src="../../gif.aspx?" id="yzm" onclick="ChangeImage();" alt="try again" style="width:50px;height:24px"></td>
        </tr>
    <tr>
    <td colSpan="3" style="width:240px;"><select name="DropDownList1" id="DropDownList1" style="width:240px;">

                                                                                                      

         

在Chrome中,我发现帖子数据是

__ VIEWSTATE =%2FwEPDwUJMjE0NzU0MTk4D2QWAmYPZBYIAgMPDxYCHgRUZXh0BQnotKLliqHpg6hkZAIEDw8WAh8ABRfnlKjmiLfvvJo1NTczL%2BWImOWtpuS6rmRkAgUPFgIeC18hSXRlbUNvdW50AgQWCGYPZBYEAgEPDxYCHwA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2K8JAtW984UHAtDAyPwJAuP1ziAC3oKsnQsC5NXi8AQCw9v50gkCz6%2BuzAH2E8Hvp9iYVUtn77jo3FKnheOfhg%3D%3D

2 个答案:

答案 0 :(得分:0)

从HTML机制的外观来看,无法处理此表单的提交。您已经知道要发布哪些数据,请尝试使用urlliburllib2发布数据来手动执行此操作:

import urllib
import urllib2
url = 'http://example.com'
form_data = {'field1': 'value1', 'field2': 'value2'}
params = urllib.urlencode(form_data)
response = urllib2.urlopen(url, params)
data = response.read()

答案 1 :(得分:0)

我在问题中找到了HTTP错误的原因。因为在python urllib中,不支持“保持活动”功能,并且获得的cookie在以下交互中无效。 我使用在IE中获得的cookie来运行代码,并获得所需的结果!

顺便说一下,Fiddler是调试HTTP请求的一个了不起的工具。