2015-11-24 74 views
2

我幾乎有這個工作,我似乎無法下載文件,當它出現。我在這裏做錯了什麼?當點擊「下載銷售報告」按鈕時,應該下載一個CSV文件,通過我的console.log()甚至不會啓動。從POST附件下載CasperJS的文件

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(); 
+0

您是否嘗試過註冊到「page.resource。收到「之前的最後'thenClick'或在'''''''''回調? –

+0

我假設你是這個意思?不,這也行不通。 '.thenClick(X( '// * [@ ID = 「ctl00_blSearchLinks」] /鋰[4]/A'),函數(){ \t casper.on( 'page.resource.received',功能(資源){ \t \t的console.log( '這裏'); \t \t如果{ \t \t \t回報(resource.stage == 「結束」!); \t \t} \t \t如果(資源。 url.indexOf('results.csv')> -1){ \t \t \t this.download(resource.url,'D:\ Jobs \ Currency \ testing \ ExportData.csv'); \t \t} \t}); })' – Adam

+0

@Adam我跑過你的腳本,發現'resource.url'不包含字符串「results.csv」,而是你下載頁面的地址。所以最好這樣做:'if(resource.contentType.indexOf('text/csv')> -1){this.download(resource.url,'./ExportData.csv'); }'但是它也行不通,因爲接受文件你必須POST表單,casper.download默認獲取它。我試圖修改[這個答案](http://bit.ly/1PYhbwh)用POST進行下載,但由於某種原因它不起作用:請求只重新加載頁面。正在進行中:http://pastebin.com/974wF4WR – Vaviloff

回答

1

我終於明白了。瓦維洛夫有99%的我需要。我只是缺少2個變量。感謝您的幫助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(); 
0

我終於得到了很長一段時間後RD的答案,我們可以使用download節點模塊下載附件如下

const fs = require('fs'); 

const download = require('download'); 

download('http://unicorn.com/foo.pdf').then(data => { 
    fs.writeFileSync('dist/foo.pdf', data); 
});` 

Link to Download NPM Module