使用Python的Mechanize提交Html表单 - 两个提交按钮没有ID

时间:2012-10-26 10:55:54

标签: mechanize-python

我正在尝试在PHP / HTML页面上提交一个HTML按钮,其中包含1个表单和两个执行不同任务的不同按钮。我需要提交第二个提交按钮。问题是这两个按钮都没有id属性,只能通过其标题标签来区分。 我需要通过选择框选择订单和订单行(我认为这是正确的)并按下表单上的第二个“开始”/“提交”按钮。 e.g。

<form>
....
<input class="button" type="submit" value="Go">
....
然后我们进一步向下......

<select class="sfield" name="last">
    <option value="0" selected="">From: First order</option>
    <option value="1">From: 19/10/2012 16:22</option>
</select>
<select class="sfield" name="items">
    <option value="1" selected="">Include: Orders and items</option>
    <option value="0">Include: Order lines only</option>
</select>
<input class="button" type="submit" onclick="this.form.action.value='export'" title="Generate CSV file" value="Go">

我的Python代码段如下......

br.form['last']  = ['0']  # Select the 'From First Order'
br.form['items'] = ['1']  # Select both Orders and Order Lines
br.submit()

# When I uncomment the line below I can process a CSV file but it just contains Orders only!
# br.open("http://www.mysite.com/admin/ordermanager.php?action=export")  

# Read and process CSV file from csv link displayed on repost of submitted form....
# this section below works fine.

for link in br.links(url_regex=r"export",nr=0):
    resp = br.follow_link(link) 
    content = resp.read()
    Last_Run_Date = strftime("%Y%m%d%H%M%S", localtime())
    with open('c:\python27\%s.csv' % Last_Run_Date, 'w') as fo: 
        fo.write(content) 

似乎没有办法在br.submit中指定title属性,例如     在Mechanize文档中,br.submit(title ='生成CSV文件')。 我假设当我想要第二个时,br.submit()正在选择第一个提交控件? 有谁知道这个聪明的方法吗?

1 个答案:

答案 0 :(得分:1)

您可以使用提交按钮的编号: br.submit(NR = 2)的 nr从第一个提交按钮的0开始

相关问题