从POST附件中下载带有CasperJS的文件 [英] Downloading a file with CasperJS from POST attachment

查看:91
本文介绍了从POST附件中下载带有CasperJS的文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我几乎可以正常工作了,当它出现时我似乎无法下载该文件.我在这里做错了什么?单击下载销售报告"按钮时,应通过我的console.log()下载CSV,甚至不会触发.

I almost have this working, I just can't seem to download the file when it comes up. What am I doing wrong here? When clicking the button "Download Sales Report" a CSV should download, by my console.log() never even fires off.

var casper = require('casper').create();
casper.start('http://www.waynecountyauditor.org/Reports.aspx?ActiveTab=Sales')
.waitForText("Accept")
.thenClick('#ctl00_ContentPlaceHolder1_btnDisclaimerAccept')
.waitForText("View Sales")
.thenClick('#ctl00_ContentPlaceHolder1_WeeklySales_fvSalesReport_btnViewSales')
.waitForText("Download Sales Report")
.thenClick(x('//*[@id="ctl00_blSearchLinks"]/li[4]/a'))
.wait(1000)
.on('page.resource.received', function(resource) {
console.log('here');
if (resource.stage !== "end") {
    return;
}
if (resource.url.indexOf('results.csv') > -1) {
    this.download(resource.url, 'D:\Jobs\Currency\testing\ExportData.csv');
}

});
casper.run();

推荐答案

我终于知道了. Vaviloff拥有我所需的99%.我只是缺少2个post变量.感谢您对Vaviloff的帮助!

I finally figured it out. Vaviloff had 99% of what I needed. I was just missing 2 post variables. Thanks for your help Vaviloff!

    // http://stackoverflow.com/questions/33903418/downloading-a-file-with-casperjs-from-post-attachment
    var casper = require('casper').create({
        verbose: true,
        logLevel: 'debug',
        pageSettings: {
              loadImages:  false         // The WebPage instance used by Casper will
            , loadPlugins: false         // use these settings
            ,  userAgent: 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_7_5) AppleWebKit/537.4 (KHTML, like Gecko) Chrome/22.0.1229.94 Safari/537.4'
        }
    });
    var x = require('casper').selectXPath;
    var utils = require('utils');

    casper.on('remote.message', function(message) {
        this.echo('LOG: ' + message);
    });

    casper.start()
    .open('http://clintonoh.ddti.net/Reports.aspx?ActiveTab=Sales')
    .waitForText("Accept")
    .thenClick('#ctl00_ContentPlaceHolder1_btnDisclaimerAccept')
    .waitForText("View Sales")
    .thenClick('#ctl00_ContentPlaceHolder1_WeeklySales_fvSalesReport_btnViewSales')
    .waitForText("Download Sales Report", function(){

        // Adapted from: http://stackoverflow.com/questions/16144252/downloading-a-file-that-comes-as-an-attachment-in-a-post-request-response-in-pha
        var res = this.evaluate(function() {

            document.getElementById('__EVENTTARGET').value='ctl00$blSearchLinks' /* Was missing these 2 */
            document.getElementById('__EVENTARGUMENT').value='4'

            var res={};
            f=document.getElementById("aspnetForm");
            var previous_onsubmit = f.onsubmit;
            f.onsubmit = function() {

                //previous_onsubmit();

                //iterate the form fields
                var post={};
                for(i=0; i<f.elements.length; i++) {
                   //console.log(f.elements[i].name + " = " + f.elements[i].value);
                   post[f.elements[i].name]=f.elements[i].value;
                }
                res.action = f.action;
                res.post = post;
                return false; //Stop form submission
            }

            // Trigger the click on the link.
            var link = document.evaluate('//*[@id="ctl00_blSearchLinks"]/li[5]/a', document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue;
            try {

                var e = document.createEvent('MouseEvents');
                e.initMouseEvent('click', true, true, window, 0, 0, 0, 0, 0, false, false, false, false, 0, null);
                link.dispatchEvent(e);

            } catch(error){
                console.log(error);
            }

            return res; //Return the form data to casper
        });

        //Start the download
        casper.download(res.action, "./ExportData.csv", "POST", res.post);    
        //casper.capture("./image.png");    

    })

    casper.run();

这篇关于从POST附件中下载带有CasperJS的文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆