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

Sul*_*lli 1 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>
Run Code Online (Sandbox Code Playgroud)

但我无法使用以下代码选择"订户"选项:

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

我究竟做错了什么?

Art*_* B. 5

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

你必须选择form使用

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

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

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);
Run Code Online (Sandbox Code Playgroud)