Python机械化-'submit'类型的两个按钮 [英] Python mechanize - two buttons of type 'submit'

查看:57
本文介绍了Python机械化-'submit'类型的两个按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个用python编写的机械化脚本,该脚本填写了网络表单,应该单击创建"按钮.但是有一个问题,该表单有两个按钮.一个用于添加附件文件",另一个用于创建".两者均为提交"类型,并且附加"按钮是列出的第一个按钮.因此,当我选择论坛并执行br.submit()时,它单击的是附加"按钮,而不是创建"按钮.广泛的Google搜索没有产生任何有用的信息来选择表单中的特定按钮.有人知道跳过第一个提交"按钮并单击第二个按钮的任何方法吗?

I have a mechanize script written in python that fills out a web form and is supposed to click on the 'create' button. But there's a problem, the form has two buttons. One for 'add attached file' and one for 'create'. Both are of type 'submit', and the attach button is the first one listed. So when I select the forum and do br.submit(), it clicks on the 'attach' button instead of 'create'. Extensive Googling has yielded nothing useful for selecting a specific button in a form. Does anyone know of any methods for skipping over the first 'submit' button and clicking the second?

推荐答案

我尝试使用nr参数,但没有任何运气.

I tried using the nr parameter, without any luck.

我能够将其与name和label参数结合使用,其中"label"似乎对应于HTML中的"value":

I was able to get it to work with a combination of the name and label parameters, where "label" seems to correspond to the "value" in the HTML:

这是我的两个提交按钮:

Here are my two submit buttons:

<input type="submit" name="Preview" value="Preview" />
<input type="submit" name="Create" value="Create New Page" />

...,这是单击第一个代码,然后返回,然后单击第二个代码的代码:

... and here's the code that clicks the first one, goes back, and then clicks the second:

from mechanize import Browser
self.br = Browser()
self.br.open('http://foo.com/path/to/page.html')
self.br.select_form(name='my_form')
self.br['somefieldname'] = 'Foo'
submit_response = self.br.submit(name='Preview', label='Preview')
self.br.back()
self.br.select_form(name='my_form')
self.br['somefieldname'] = 'Bar'
submit_response = self.br.submit(name='Create', label='Create New Page')

有一个变体也对我有用,其中提交"按钮的名称"是相同的,例如:

There's a variant that also worked for me, where the "name" of the submit button is the same, such as:

<input type="submit" name="action" value="Preview" />
<input type="submit" name="action" value="Save" />
<input type="submit" name="action" value="Cancel" />

self.br.select_form(name='my_form')
submit_response = self.br.submit(name='action', label='Preview')
self.br.back()
submit_response = self.br.submit(name='action', label='Save')

重要说明-仅我可以在清理页面其余部分的HTML后使所有此多重提交按钮代码正常工作.

IMPORTANT NOTE - I was only able to get any of this multiple-submit-button code to work after cleaning up some HTML in the rest of the page.

具体来说,我不能拥有<br/>-相反,我必须拥有<br /> ...,而且,更不合理的是,两个提交按钮之间没有任何东西.

Specifically, I could not have <br/> - instead I had to have <br /> ... and, making even less sense, I could not have anything between the two submit buttons.

我花了两个多小时来寻找机械化/ClientForm的错误归结为:

It frustrated me to no end that the mechanize/ClientForm bug I hunted for over two hours boiled down to this:

<tr><td colspan="2"><br/><input type="submit" name="Preview" value="Preview" />&nbsp;<input type="submit" name="Create" value="Create New Page" /></td></tr>

(全部一行)不起作用,但是

(all on one line) did not work, but

<tr><td colspan="2"><br />
<input type="submit" name="Preview" value="Preview" />
<input type="submit" name="Create" value="Create New Page" /></td></tr>

工作正常(在多行上也没关系).

worked fine (on multiple lines, which also shouldn't have mattered).

我喜欢机械化,因为它易于安装(只需将文件复制到我的include目录中)并且使用起来非常简单,但是除非我缺少主要的东西,否则我认为这样的错误很糟糕-我根本想不出为什么第一个示例应该失败而第二个示例应该起作用的充分理由.

I like mechanize because it was easy to install (just copy the files into my include directory) and because it's pretty simple to use, but unless I'm missing something major, I think that bugs like this are kind of awful - I can't think of a good reason at all why the first example there should fail and the second should work.

而且,偶然地,我还发现了另一个机械化错误,其中<p>中包含的<textarea>未被识别为有效控件,但是一旦将其从<p>容器中取出,就可以了. .我检查了是否允许将textarea 包含在其他块级元素(例如<p>)中.

And, incidentally, I also found another mechanize bug where a <textarea> which is contained within a <p> is not recognized as a valid control, but once you take it out of the <p> container it's recognized just fine. And I checked, textarea is allowed to be included in other block-level elements like <p>.

这篇关于Python机械化-'submit'类型的两个按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆