【问题标题】:Using Casperjs to Submit Form with no ID or Name使用 Casperjs 提交没有 ID 或名称的表单
【发布时间】:2017-01-12 13:58:48
【问题描述】:

这是我第一次使用 Casperjs。我有一个没有 ID 或名称但需要提交用户名和密码的表单。

在 HTML 正文中有一个 onload="init(document.forms[0]);"

形式:

<form method="post">
                <table border="0" cellspacing="0" cellpadding="0">
                        <tbody><tr>
                        <script>
                                document.write('<td class="username">');
                                document.write(langstr[ui_CurrentLanguage].login_username);
                                document.write('&nbsp;:&nbsp;');
                                document.write('</span>');
                                document.write('<td colspan="2"><input type="text" class="inputname" id="user_name" name="user_name" onKeyPress="if(event.keyCode == \'13\') Validate();" size="20" maxlength="65" autocomplete="off"></td>');
                        </script><td class="username">USERNAME&nbsp;:&nbsp;</td><td colspan="2"><input type="text" class="inputname" id="user_name" name="user_name" onkeypress="if(event.keyCode == '13') Validate();" size="20" maxlength="65" autocomplete="off"></td>
                        </tr>
                        <tr>
                        <script>
                                document.write('<td class="password">');
                                document.write(langstr[ui_CurrentLanguage].login_password);
                                document.write('&nbsp;:&nbsp;');
                                document.write('</span>');
                                document.write('<td colspan="2"><input type="password" class="inputpwd" id="user_passwd" name="user_passwd" onKeyPress="if(event.keyCode == \'13\') Validate();" size="20" maxlength="64" autocomplete="off"></td>');
                        </script><td class="password">PASSWORD&nbsp;:&nbsp;</td><td colspan="2"><input type="password" class="inputpwd" id="user_passwd" name="user_passwd" onkeypress="if(event.keyCode == '13') Validate();" size="20" maxlength="64" autocomplete="off"></td>
                        </tr>
                </tbody></table>

我在引用表单时遇到问题,以便我可以在 casperjs 中使用填充方法。关于如何获取此表单的引用的任何想法?

谢谢。

【问题讨论】:

    标签: javascript dom casperjs


    【解决方案1】:

    您需要使用sendKeys()填写表格并发送密钥“13”(回车):

    var casper = require('casper').create(), utils = require('utils');
        casper
    
    .start('http://',function(){
        this.sendKeys('#user_name',  'your_name', {keepFocus: true});
        this.sendKeys('#user_passwd','your_pass', {keepFocus: true});
        this.sendKeys('#user_passwd', casper.page.event.key.Enter , {keepFocus: true});
    
        this.wait(3000,function(){
        this.capture('test.png');
        utils.dump(this.getTitle());
    });
    })
        .run();
    

    另见:this issue

    【讨论】:

    • 谢谢!我会试一试,然后回复你。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-08-31
    • 1970-01-01
    • 1970-01-01
    • 2012-12-07
    • 2015-11-20
    • 1970-01-01
    • 2018-07-14
    相关资源
    最近更新 更多