开发者

Mechanize submit

We have a form which has a few separate submit buttons which do different actions. The problem is I have a couple of buttons which have the following HTML:

<input type="submit" name="submit" value="Submit" class="submitLink" title="Submit" />
<开发者_运维百科;input type="submit" name="submit" value="Delete" class="submitLink" title="Delete" />

Now you can't locate an element by value with the standard find_control function. So I wrote a predicate function which would find my element, which I was then hoping to click in the following manner:

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

However both submit and click internally call find element, and neither method allows you to add in the predicate keyword, so a call like this doesn't work either:

self.br.submit(predicate=submit_button_finder)

Is there anything I'm missing?!?

Update:

Added a helper function to retrieve all elements which meet the criteria as such:

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

Then replaced the following lines:

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

With:

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()


A fairly ghetto workaround is to manually iterate through all the controls in the form in question and then remove the controls from the form that you do not want based on criteria. For instance:

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

The best idea here is to limit the controls that you're iterating through to SubmitControl objects ONLY. This way, you'll limit the form to one submit button, and the browser.submit() method will have no choice of what to click.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜