簡體   English   中英

使用CasperJS單擊鏈接后下載CSV

[英]Download CSV after clicking link using CasperJS

我創建了一個腳本,該腳本登錄到我的銀行帳戶,導航到交易頁面,然后嘗試下載所有交易數據的CSV。 但是,單擊“下載”按鈕后,將永遠不會下載資源。 單擊按鈕時調用的資源是“ download.qfx”,並且每次都會生成一個不同的文件名。 任何幫助,將不勝感激。

// When download page loads, click the appropriate settings and download transactions
casper.then(function(){
    this.waitForSelector("#transactionPeriod", function() {
       this.evaluate(function() {
           document.querySelector('#transactionPeriod').selectedIndex = 0; //it is obvious
           return true;
        });
        this.clickLabel("Spreadsheet (Comma Separated Values)", "label");
    });
});
// Click the download button
casper.then(function(){
   casper.click(x("//a[contains(text(), 'Download')]"));
});
// Save the download file
casper.then(function(){
         casper.download("https://secure.capitalone360.com/myaccount/download.qfx", "export.csv");
});

這是檢查員的圖像,以防這些細節中的任何一個有助於弄清問題。 在此處輸入圖片說明

更新:我也嘗試過,但是單擊“下載”事件后調試器中沒有輸出。

casper.then(function(){
   casper.click(x("//a[contains(text(), 'Download')]"));
});

casper.on('resource.received', function(resource) {
    if (resource.stage !== "end") {
        console.log("resource.stage !== 'end'");
        return;
    }
    if (resource.url.indexOf('download.qfx') > -1) {
        console.log("Downloading csv file");
        this.download(resource.url, 'ExportData.csv');
    }
});

此外,如果鍵入console.log(resource.url),則永遠不會看到download.qfx。 也許這暗示出什么問題了?

僅單擊鏈接似乎並未導致應該發生的onClick javascript調用。 所以,然后我測試了:

casper.then(function(){
   //casper.click(x("//a[contains(text(), 'Download')]"));
    casper.evaluate(function(){
        urchinTracker('/download_transactions/continue');
        submitForm('download');
        return false; 
    });
});

casper.on('resource.received', function(resource) {
    //console.log(resource.url);
    if (resource.stage !== "end") {
        return;
    }
    if (resource.url.indexOf('download.qfx') !== -1) {
        this.download(resource.url, 'ExportData.csv');
    }
});

因此,由於某種原因,有必要分別調用onClick函數。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM