2012-07-27 16 views
1

我正在使用機械化來處理表單。我已經解析使用機械化的形式和輸出來如下:在rails中使用機械化刪除類的實例

{forms 
#<Mechanize::Form 
{name nil} 
{method "POST"} 
{action "/dashboard/checks/50114dbeae6f61b428000ad8"} 
{fields 
    [hidden:0x60c476a type: hidden name: _method value: put] 
    [text:0x60c4616 type: text name: check[name] value: Testing] 
    [text:0x60c4512 type: text name: check[url] value: http://www.pintile.com] 
    [text:0x60c445e type: text name: check[interval] value: 120] 
    [text:0x60c435a type: text name: check[maxTime] value: 1500] 
    [textarea:0x60c4116 type: name: check[tags] value: ]} 
{radiobuttons} 
{checkboxes} 
{file_uploads} 
{buttons 
    [button:0x60c3d88 type: submit name: value: ] 
    [button:0x60c3d24 type: submit name: delete value: ] 

有這種形式 保存更改(1日),刪除(2日)2個按鍵, 我用以下代碼保存更改並且工作正常:

form.field_with(:name => "check[name]").value = "Testing" 
button = form.buttons.first 
agent.submit(form, button) 

更改已成功保存。但是,當我嘗試刪除使用下面的代碼它不起作用:

button = form.buttons.first 
agent.submit(form, button) 

它什麼都不做。請幫我解決這個問題。

回答

0

如果網站是一個典型的rails形式,刪除按鈕很可能是一個JavaScript動作。 Mechanize不支持JavaScript。您可能希望使用諸如capybara之類的東西,而不是Web Kit驅動程序,它具有完整的JavaScript支持和您在機械化中已具備的所有功能。

+0

嘿Tanzeeb,我非常感謝你的迴應。你是正確的**刪除**按鈕是一個JavaScript操作。現在我想觸發這個JavaScript函數。我尋找[鏈接](https://github.com/thoughtbot/capybara-webkit),但這是爲了測試。建議我多點。 – 2012-07-27 09:50:05

+0

下面是一個很好的例子,讓你開始:http://www.chadcf.com/blog/using-capybara-javascript-capable-replacement-mechanize – 2012-07-27 15:58:49

0

下面工作的代碼片段與link.Have以及你試圖點擊

agent.get("http://your url") 

agent.page.link_with(:text => "link name").click 

試試看吧.. 否則如上面提到可以使用水豚

+0

問題是它不是一個鏈接,所以點擊不起作用。它由一個javascript函數觸發。 – 2012-07-31 07:29:42

0

刪除是最後一個按鈕。因此,你想:

button = form.buttons.last 
agent.submit(form, button) 

或者更簡單地說:

form.submit form.buttons.last 
0

如果機械化是對你不夠好,你不需要任何JavaScript的支持,將帶來水豚,你應該能夠仿效什麼Javascript的確如此。在這種情況下,源將需要更好地知道什麼附加,但我猜在提交之前,隱藏域_method的值被delete取代。