2014-05-08 20 views
1

我有以下形式:如何選擇與Casperjs下拉選項和fillSelectors

<form name="security_page_toplevel_page_itsec_settings" method="post" action="options.php"> 
    <select id="itsec_strong_passwords_roll" name="itsec_strong_passwords[roll]"> 
     <option value="admin">admin</option> 
     <option value="subscriber">subscriber</option> 
    </select> 
</form> 

,但我無法選擇用下面的代碼的「用戶」選項:

this.fillSelectors('form#security_page_toplevel_page_itsec_settings', { 
    'select[name="itsec_strong_passwords[roll]"]': 'subscriber' 
}, true); 

什麼時我做錯了?

回答

5

表單的name屬性與id屬性不同。

你必須使用

this.fillSelectors('form[name="security_page_toplevel_page_itsec_settings"]', { 
    'select[name="itsec_strong_passwords[roll]"]': 'subscriber' 
}, true); 

如果這不起作用選擇form,你可以嘗試明確設置在頁面上下文中選擇選項:

var index = 2; // the index to select, you may calculate the index programatically from the option value 
this.evaluate(function(index) { 
    var sel = document.querySelector('form[name="security_page_toplevel_page_itsec_settings"] select[name="itsec_strong_passwords[roll]"]'); 
    sel.selectedIndex = index; 
    sel.onchange(); 
}, index); 
+0

我用你的語法,但它仍然不起作用。必須有別的東西,但我不知道爲什麼。我嘗試過不驗證表單(使用「false」參數),然後通過單擊保存按鈕(我更喜歡使用此方法)來明確驗證表單,但它仍然不起作用。 – Sulli

+0

我增加了另一種選擇索引的可能性。但我也試過你的代碼[這個小例子](https://gist.github.com/anonymous/ea5a33589a62a4a1cab0)。這個對我有用。你可以自己嘗試。 –

+0

其實這兩個解決方案的工作!我的問題是,我試圖通過用F5鍵重新加載我的頁面來查看'fillSelectors'的效果,但由於某些原因,更改沒有以這種方式顯示!只有當我明確重新點擊鏈接到我的網頁時,所顯示的變化......很多浪費時間。截圖讓我看到你的功能確實正常工作。不管怎麼說,還是要謝謝你! – Sulli