如何使用Casperjs和fillSelectors选择下拉选项

时间:2014-05-08 17:09:47

标签: javascript forms html-select casperjs

我有以下表格:

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

但我无法选择&#34;订阅者&#34;选项包含以下代码:

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

我做错了什么?

1 个答案:

答案 0 :(得分:5)

表单的name属性与id属性不同。

您必须使用

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

如果这不起作用,您可以尝试在页面上下文中明确设置选择选项:

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