【问题标题】:How to select dropdown option with Casperjs and fillSelectors如何使用 Casperjs 和 fillSelectors 选择下拉选项
【发布时间】:2014-05-08 17:09:46
【问题描述】:

我有以下表格:

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

我做错了什么?

【问题讨论】:

    标签: javascript forms html-select casperjs


    【解决方案1】:

    表单的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);
    

    【讨论】:

    • 我用过你的语法,但还是不行。一定有别的东西,但我不知道为什么。我试过不验证表单(使用“false”参数),然后只通过单击保存按钮显式验证表单(我更喜欢使用这种方法),但它仍然不起作用。
    • 我添加了另一种选择索引的可能性。但我也用this minimal example 试过你的代码。这个对我有用。你可以自己试试。
    • 实际上这两种解决方案都有效!我的问题是我试图通过使用 F5 键重新加载我的页面来查看fillSelectors 的效果,但由于某些原因,更改没有以这种方式显示!只有当我明确地重新单击指向我的页面的链接时,才会显示更改……为此浪费了很多时间。屏幕截图让我看到您的功能确实正常工作。还是谢谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-06-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-08-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多