编程语言
首页 > 编程语言> > python-机械化提交

python-机械化提交

作者:互联网

我们有一个表单,其中包含一些单独的提交按钮,它们可以执行不同的操作.问题是我有几个带有以下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()

解决方法:

贫民窟的一种解决方法是手动遍历有问题的表单中的所有控件,然后根据条件从不需要的表单中删除这些控件.例如:

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

最好的想法是将要迭代的控件仅限制为SubmitControl对象.这样,您将表单限制为一个提交按钮,并且browser.submit()方法将无法选择要单击的内容.

标签:testing,mechanize,python
来源: https://codeday.me/bug/20191208/2094777.html