2014-03-25 369 views
3

我有html代碼如下選擇名稱與:(我需要填寫更多的選擇包括到形式mainForm):如何填寫選擇框,當選擇名稱和ID包括「:」

<select id="mainForm:projectSelectOneMenu" name="mainForm:projectSelectOneMenu" class="selectOne" size="1" onchange="A4J.AJAX.Submit('mainForm',event,{'similarityGroupingId':'mainForm:j_id416','parameters':{'mainForm:j_id416':'mainForm:j_id416'} ,'containerId':'mainForm:trainPeriodCriteriaRegion'})"> 
    <option value="">--- All ---</option>  

我使用這個代碼:

this.fillSelectors('form#mainForm', { 
    'select[name="mainForm:projectSelectOneMenu"]' : "3" 
}, false); 
this.wait(6000,function(){ 
    this.echo("i saw my new value"); 
}); 

我有這樣的錯誤:

CasperError: Errors encountered while filling form: no field matching css selector "select[name="mainForm:projectSelectOneMenu"]" in form.

我也用其他方式來填充選擇,但似乎我無法與我的腳本識別選擇名稱。

好吧,現在它的工作,我填補了我需要的所有選擇。現在,最後一步就是點擊按鈕的MainForm:updateTrainPeriodCriteriaButton:

</table> 
<br><input id="mainForm:updateTrainPeriodCriteriaButton"  
name="mainForm:updateTrainPeriodCriteriaButton" value="Apply" onclick="A4J.AJAX.Submit('mainForm',event,{'similarityGroupingId':'mainForm:j_id459','parameters': {'mainForm:j_id459':'mainForm:j_id459'} ,'containerId':'mainForm:trainPeriodCriteriaRegion'})" type="submit"> 
<hr> 

使用代碼:

console.log("id = mainForm:updateTrainPeriodCriteriaButton ? -> "+this.getElementAttribute('input[name="mainForm:updateTrainPeriodCriteriaButton"]','id')); 

輸出是正確的: ID =的MainForm:updateTrainPeriodCriteriaButton? - >的MainForm:updateTrainPeriodCriteriaButton

但我不能點擊它:

casper.then(function(){  
    this.waitUntilVisible('#mainForm', function(){ 
    this.fill('form#mainForm', { 
     'mainForm:criteriaStartCalendarInputDate': "2014/03/27 05:00", 
     'mainForm:criteriaEndCalendarInputDate': "2014/03/27 07:59", 
    }, false); 
}); 
//Click 
this.thenClick('#mainForm:updateTrainPeriodCriteriaButton'); 
    this.then(function(){ 
     this.wait(10000, function(){ 
      this.capture('After_click.png'); 
     }); 
    });  
}); 

錯誤:

CasperError: Cannot dispatch mousedown event on nonexistent selector: #mainForm:updateTrainPeriodCriteriaButton

回答

2

編輯:我試圖用「選擇[名稱=「的MainForm:projectSelectOneMenu 「]」: 「3」 和它的作品...

嘗試使用:

//Emitted when a screenshot image has been captured. 
casper.on('capture.saved', function(targetFile) { 
    this.echo('screen properly done at ' + targetFile); 
}); 

this.fillSelectors('form#mainForm', { 
    'select[name*="mainForm"]' : "3" 
    }, false); 
this.wait(2000,function(){ 
    this.echo("i saw my new value"); 
    this.capture('imgFormCasper.jpg', undefined, { 
     format: 'jpg', 
     quality: 75 
    }); 
});  

也許是:

this.fillSelectors('form#mainForm', { 
    'select[name*="projectSelectOneMenu"]' : "3" 
    }, false); 
this.wait(2000,function(){ 
    this.echo("i saw my new value"); 
    this.capture('imgFormCasper.jpg', undefined, { 
     format: 'jpg', 
     quality: 75 
    }); 
});  

那麼試試這個指令:

console.log("id = mainForm:projectSelectOneMenu ? -> "+this.getElementAttribute('select[name="mainForm:projectSelectOneMenu"]','id'))‌​; 

你應該有: bug casper 如果你有沒有一個IDE,這裏測試你的代碼,以確保您沒有語法錯誤:http://esprima.org/demo/validate.html

+0

感謝@Fanch爲您的答案,但似乎2解決方案不工作:CasperError:填寫表單時遇到錯誤:沒有字段匹配css選擇器「選擇[名稱* =」mainForm「]」 CasperError:填寫表單時遇到的錯誤:沒有字段匹配的CSS選擇器「select [name * =」projectSelectOneMenu「]」形式 – Quentin

+0

我試着'select [name =「mainForm:projectSelectOneMenu」]':「3」,它的工作原理...所以這不是問題,你的選擇器似乎沒問題。你的頁面上只有一個表單,id = mainForm? – Fanch

+0

是的,但有很多項目(選擇器等),稱爲mainForm:something_else。 mainForm只有一個:

Quentin