机械化提交

时间:2010-12-21 19:30:50

标签: python testing mechanize

我们有一个表单,其中有一些单独的提交按钮,可以执行不同的操作。问题是我有几个按钮具有以下HTML:

<input type="submit" name="submit" value="Submit" class="submitLink" title="Submit" />
<input type="submit" name="submit" value="Delete" class="submitLink" title="Delete" />

现在,您无法使用标准find_control函数按值查找元素。所以我写了一个谓词函数,它会找到我的元素,然后我希望以下面的方式点击:

submit_button = self.br.form.find_control(predicate=submit_button_finder)
self.br.submit(submit_button)

然而,提交和单击内部调用find元素,并且这两种方法都不允许您添加谓词关键字,因此这样的调用也不起作用:

self.br.submit(predicate=submit_button_finder)

有什么我想念的吗?!?

更新

添加了一个辅助函数来检索符合条件的所有元素:

def find_controls(self, name=None, type=None, kind=None, id=None, predicate=None, label=None):

  i = 0
  results = []

  try :
    while(True):
      results.append(self.browswer.find_control(name, type, kind, id, predicate, label, nr=i))
      i += 1
  except Exception as e: #Exception tossed if control not found
    pass
  return results

然后更换以下行:

submit_button = self.br.form.find_control(predicate=submit_button_finder)
self.br.submit(submit_button)

使用:

submit_button = self.br.form.find_control(predicate=submit_button_finder)
submit_buttons = self.find_controls(type="submit")
for button in submit_buttons[:]:
  if (button != submit_button) : self.br.form.controls.remove(button)
self.br.submit()

1 个答案:

答案 0 :(得分:3)

一个相当贫民窟的解决方法是手动迭代相关表单中的所有控件,然后根据条件从表单中删除控件。例如:

for each in form.controls[:]:
  if each not "some criteria":
    form.controls.remove(each)

这里最好的想法是将您正在迭代的控件限制为SubmitControl对象。这样,您就可以将表单限制为一个提交按钮,而browser.submit()方法将无法选择要点击的内容。