casperjs填写表格不起作用

时间:2016-07-21 13:25:26

标签: javascript html phantomjs casperjs

我遇到了casperjs的问题。

我想填写表单,但casperjs说找不到表单...在代码中我看不到表单名称所以我试图使用formid但没有成功

以下是我要填写表格的网站的源代码

<tr class="wcontent" valign="top">
<td></td>
<td id="wcontent_login" class="widgetMiddleMiddle" style="display: block; height: 213px;">
<iframe frameborder="no" src="/WebInt.nsf/loginWidget.xsp?widgetUniqueId=anonymous-login-default" scrolling="no">
<!DOCTYPE html>
<html lang="de">
<head>
    <body class="xspView tundra">
    <form id="view:_id1" class="xspForm" enctype="multipart/form-data" action="/GI7/WebInt.nsf/loginWidget.xsp?widgetUniqueId=anonymous-login-default" method="post">
    <div id="inactive_loginWidget">
    <div class="inactive_loginLeft">Anmeldung</div>    
    <div class="inactive_loginRight">
    <div style="height:27px;background-color:#004178;color:white;padding:10px;    width:227px;margin-bottom:5px;text-align:center;">
    <div>
        <input id="username" type="text" placeholder="Benutzername" onkeypress="return handleKeyPressed(event)" onfocus="var elem =     document.getElementById(''); if(elem != null) {hideLabel(elem, 'username');}" onblur="var elem = document.getElementById(''); if(elem != null) {if(document.getElementById('username').value == '') { showLabel(elem); } else { hideLabel(elem); }}" name="username" autocomplete="off" vk_19ae7="subscribed">
    </div>
    <div class="cleaner3px"></div>
<div>
    <div class="cleaner3px"></div>
    <div title="Anzahl der Spalten für die Darstellungsbreite des Browsers einstellen.">
    <div class="cleaner10px"></div>
    <div class="button" style="margin-left: -5px; cursor: default;"     onmouseover="setHoverEffectButton(this, '');"     onmouseout="resetHoverEffectButton(this, '');"     onmousedown="setPressedButtonEffect(this, '');"     onclick="handleKeyPressed(event, true)">
    </div>
    </div>
    <script type="text/javascript">
    <input id="view:_id1__VUID" type="hidden" value="!1k37604fezbng88zt78ksqv5x!" name="$$viewid">
    <input type="hidden" name="$$xspsubmitid">
    <input type="hidden" name="$$xspexecid">
    <input type="hidden" name="$$xspsubmitvalue">
    <input type="hidden" name="$$xspsubmitscroll">
    <input type="hidden" value="view:_id1" name="view:_id1">
    </form>
    </body>
</html>
</iframe>

我的casperjs代码

var casper = require("casper").create({
    verbose: true,
    logLevel: "debug"
});
function wait5seconds() {
    casper.wait(5000, function() {
    this.echo('2');    
    });
}

//    casper.options.waitTimeout = 40000;

casper.start("https://Testseite.de/WebInt.nsf/loginWidget.xsp", function() {
    this.echo("Page title: " + (this.getTitle()));
    wait5seconds();
});

casper.then(function(){
    this.fillSelector('Formname ????', {
    'username':    'Hans',
    'password':    'Wurst'
    }, true);
});

casper.then(function() {
        casper.capture('Test2.png');
});

casper.then(function() {
    this.evaluate(function(){
    //trigger click event on submit button
    document.querySelector('input[type="submit"]').click();
    });
});
casper.then(function() {
    casper.capture('Test3.png');
});

casper.run();

我也尝试用queryselector解决我的问题,但仍然没有成功:(

3 个答案:

答案 0 :(得分:0)

我认为你错过了'形式;来自选择器。

有些事情:

GeckoHtmlElement headElement = Navegador.Document.GetElementsByTagName("head")[0];
GeckoHtmlElement scriptElement = Navegador.Document.CreateHtmlElement("script");
scriptElement.TextContent = "('#test').on('submit',(function(e) { e.preventDefault(); $.ajax({ url: 'test.php', type: 'POST', data: new FormData(this),  contentType: false, cache: false, processData: false, success: function(data){ $('#ajax_response').html(data); },  error: function() { } }); }));";
headElement.AppendChild(scriptElement);

这个问题可能有所帮助:How to use fillSelectors in casperjs

答案 1 :(得分:0)

是的,但每次我开始测试时,它都会在第4步中断:

casper.then(function(){
     this.echo("Formstart");
    this.fillSelector('form#view:_id1', {
            'username':    'XX',
            'password':    'XX'
    },true);
     this.echo("Formend");
});

Formstart有效,但Formend没有打印,所以错误必须在那里.... 我现在改为:

casper.then(function(){
    this.echo("Formstart");
    this.fillSelector('form#view:_id1', {
            'input[name="username"]':    'XX',
            'input[name="password"]':    'Xx'
    },true);
    this.echo("Formend");

});

但仍然没有成功:(

答案 2 :(得分:0)

最后我用调试器和Post请求解决了它:

casper.open('https://XXX/WebInt.nsf',
{
    method: 'post',
    data: {'username':'XX','password':'XX'}
    }, function(response){
    if(response.status == 200){
        require('utils').dump(this.page.content);
    }
});

:)