2010-12-21 303 views
3

我們有一個表單,它有幾個單獨的提交按鈕,可以執行不同的操作。問題是我有幾個按鈕,它們具有以下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) 

但是都提交併單擊內部調用發現元素,並沒有方法可以讓你的加入謂詞關鍵字,所以像這樣的調用也不起作用:

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

我很確定你不應該在頁面上有兩個同名的按鈕。這可能是你的問題的一部分。 – 2010-12-21 19:52:16

+0

@Alex我不幸的是沒有控制,但。 – Scott 2010-12-21 19:54:49

回答

3

一個相當貧民窟的解決方法是手動遍歷有問題的表單中的所有控件,然後根據條件從不需要的表單中刪除控件。例如:

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

這裏最好的辦法是限制你迭代到SubmitControl對象的控件。這樣,您將限制表單爲一個提交按鈕,並且browser.submit()方法將無法選擇要單擊的內容。