简体   繁体   English

casperjs按钮单击不导航到下一页

[英]casperjs button click doesn't navigate to next page

I have a page that contains the HTML form with javascript setup like if you click on a button with someid, the form gets submitted. 我有一个页面,其中包含带有javascript设置的HTML表单,如果您单击带有someid的按钮,表单将被提交。 I verified this by firing this in browser console: 我在浏览器控制台中解决了这个问题:

document.getElementById("arcotsubmit").click() 的document.getElementById( “arcotsubmit”)。点击()

As soon as it gets fired, the url changes and form gets submitted. 一旦被触发,url就会更改并且表单会被提交。

Now i tried to replicate this with casper.js: 现在我试图用casper.js复制这个:

var casper = require('casper').create({});


casper.start('https://cib.icicibank.com/corp/BANKAWAY?Action.CorpUser.Init1.001=Y&AppSignonBankId=ICI&AppType=corporate', function() {
    this.echo(this.getTitle());
});

casper.then(function(){

    this.click("#arcotsubmit");

});

casper.then(function(){

    console.log(this.getCurrentUrl())
});

casper.run();

It stays on the same URL and downloads the same page. 它保留在同一个URL上并下载相同的页面。 I want to download the html that appears after the button gets clicked by casper. 我想下载casper点击按钮后出现的html。 The URL is live and can be tested directly. 该URL是实时的,可以直接测试。

My point is if i can use this command in browser console 我的观点是,如果我可以在浏览器控制台中使用此命令

document.getElementById("arcotsubmit").click()

and make it redirect, why i am unable to do it with this.click("#arcotsubmit") in casperjs? 并使其重定向,为什么我无法用casperjs中的this.click(“#arcotsubmit”)来做它?

It is submit instead click for redirecting. 它是提交而不是点击重定向。 The default event for input[type=image] is submit so that try this: 输入[type = image]的默认事件是提交,所以尝试这样做:

casper.test.begin('Test page.', 2, function suite(test) {
    casper.start(
        'https://cib.icicibank.com/corp/BANKAWAY?Action.CorpUser.Init1.001=Y&AppSignonBankId=ICI&AppType=corporate',
        function() {
            this.echo(this.getTitle());
            test.assertUrlMatch(/BANKAWAY?/, 'Current location is ' + this.getCurrentUrl());
        }
    );

    casper.then(function(){
        this.fill('#rt', {}, true);
        this.wait(2000, function() {
            test.assertUrlMatch(/BANKAWAY;/, 'New location is ' + this.getCurrentUrl());
        });
    });

    casper.run(function() {
        test.done();
    });
});

It will be passed. 它将通过。 Test Result Screen Shot 测试结果屏幕截图

Possible quick fix. 可能的快速修复。 If Casper's click isn't working, but your code works in console of the browser, try using Casper's evaluate function. 如果Casper的点击不起作用,但您的代码在浏览器的控制台中工作,请尝试使用Casper的评估功能。

casper.then(function() {
  this.evaluate(function() {
    document.getElementById("arcotsubmit").click();
  });
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM