0

我正在使用 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代码:

TITLE function 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%2BWImOWtpuS6rmRkAgUPFgIeC18hSXRlbUNvdW50AgQWCGYPZBYEAgEPDxYCHwAFLOWFseWPluWPt1s3NV3lvZPliY3lj7dbMTAyMF3nrYnlvoXkurrmlbBbNTVdZGQCAw8PFg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fhg%3D%3D__EVENTVALIDATION=%2FwEWCwLc%2Bdl4AqPSn9wLAuqo1NQIAq3v2K8JAtW984UHAtDAYPwJAuP1ziAC3oKsnQsC5NXi8AQCw9v50gkCz6%2BuzAH2E8Hvp9iYVUtn77jo3FKnheOfhg%3D%3D__EVENTVALIDATION=%2FwEWCwLc%2Bdl4AqPSn9wLAuqo1NQIAq3v2K8JAtW984UHAtDAYPwJAuP1ziAC3oKsnQsC5NXi8AQCw9v50gkCz6%2BuzAH2E8Hvp9iYVUtn77jo3FKnheOfhg%3D%3D

4

2 回答 2

0

我在我的问题中找到了 HTTP 错误的原因。由于在python urllib中,不支持“keep-alive”特性,在后面的交互中获取的cookie是无效的。我用IE中获取的cookie运行代码,得到了想要的结果!

顺便说一下,Fiddler是一个很棒的调试 HTTP 请求的工具。

于 2013-08-14T09:42:01.313 回答
0

从 HTML mechanize 的外观来看,无法处理此表单的提交。正如您已经知道要发布哪些数据尝试通过使用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()
于 2013-08-13T12:32:15.970 回答