BCR
BCR

Reputation: 982

Casperjs click won't open 'download csv' os window

Current software stack: Casperjs 1.1 beta, Phantomjs 1.9.7, Windows 8.1 64 bit, Visual Studio 2013

I've been trying to use casperjs to automate a simple process. I want to navigate to the following url:

http://financials.morningstar.com/income-statement/is.html?t=TSLA&region=usa&culture=en-US

From there I want to simulate a click event on the Export button, found within the table header, and download the .csv file from the os popup window.

Thinking this would be relatively simple was foolish of me. Thus far I have been able to navigate to the page, confirm it is the correct page, use either a CSS or Xpath Selector to locate the proper element. Problem is when I use the click or thenClick methods the script runs with NO errors however the screen capture does not show a click event like a popup window etc.

I moved on to trying the mouseEvent method to simulate the click Export however this time the script runs but the output returns a warning from Phantom - Loading resource failed with status=fail (HTTP 200).

Also of note is the inspect element value using Chrome within the aforementioned page: <a href="javascript:SRT_stocFund.Export()" class="rf_export"></a>

I continued to dig into the page files and located the definition of the javascript export function:

SRT_stocFund.Export = function () {
    //return false;
    var params = this.GetPara();
    document.location = hostPath+"/ajax/ReportProcess4CSV.html?" + params+"&denominatorView="+denominatorView+"&number="+number;
};

See my current code below. If necessary I can provide some excerpts from the cmd shell debug output.

var casper = require('casper').create({
    pageSettings: {
        loadImages: false,
        loadplugins: false,
    },
    verbose: true,
    logLevel: 'debug',
    userAgent: 'Mozilla/5.0 (Windows NT 6.3; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/37.0.2049.0 Safari/537.36'
});


phantom.cookiesEnabled = true;
var utils = require('utils');
var fs = require('fs');
var cookies = JSON.stringify(phantom.cookies);
fs.write('cookies.txt', cookies, 644);

var x = require('casper').selectXPath;

// which 3 statements; select from:
// 'income-statement/is'
// 'balance-sheet/bs'
// 'cash-flow/cf'

var statement = 'income-statement/is';
var stock_here = 'TSLA';

// morningstar url
var url = 'http://financials.morningstar.com/' + statement + '/is.html?t=' + stock_here + '&region=usa&culture=en-US';

// open url
casper.start(url, function () {
    this.echo(this.getTitle());
    console.log('site load...');
    if (this.visible('#sfcontent > div.rf_ctlwrap > div.rf_ctl2_opt > div.exportButton > span > a')) {
        this.echo("This element selector expression is FOUND");
    } else {
        this.echo("This element selector expression is MISSING");
    }
}).viewport(1200, 1000);

casper.waitForSelector('#sfcontent > div.rf_ctlwrap > div.rf_ctl2_opt > div.exportButton > span > a', function () {
    casper.mouseEvent('click', '#sfcontent > div.rf_ctlwrap > div.rf_ctl2_opt > div.exportButton > span > a');
    casper.wait(1000);
    casper.capture('clicked.png');
});

casper.run();

Upvotes: 1

Views: 733

Answers (1)

scandinavian_
scandinavian_

Reputation: 2576

Do you have to use casperjs? You can just fetch the direct URL for the CSV, this seems to work as intended:

import urllib2
import csv
import StringIO

url = "http://financials.morningstar.com/ajax/ReportProcess4CSV.html?&t=XNAS:{}&region=usa&culture=en-US&cur=USD&reportType=is&period={}&dataType=A&order=asc&columnYear=5&rounding=3&view=raw&denominatorView=raw&number=3"

stock = "TSLA"
period = 12 #In months, 12 = annual, 3 = quarterly, etc.    

request = urllib2.urlopen(url.format(stock, period))

filename = request.info()['Content-Disposition'][21:-1]
data = request.read()

f = StringIO.StringIO(data)
reader = csv.reader(f)

for row in reader:
    print row

For python 3.4:

import urllib.request
import io   
import csv

url = "http://financials.morningstar.com/ajax/ReportProcess4CSV.html?&t=XNAS:{}&region=usa&culture=en-US&cur=USD&reportType=is&period={}&dataType=A&order=asc&columnYear=5&rounding=3&view=raw&denominatorView=raw&number=3"

stock = "TSLA"
period = 12 #In months, 12 = annual, 3 = quarterly, etc.    

request = urllib.request.urlopen(url.format(stock, period))

filename = request.info()['Content-Disposition'][21:-1]
data = str(request.read())

f = io.StringIO(data)
reader = csv.reader(f)

for row in reader:
    print(row)

Upvotes: 1

Related Questions