Submitting a form and uploading a file using CasperJS

Note. I have already reviewed these links:

CasperJS and file upload via iFrame and JavaScript

Submitting a form using casperjs

I thought they were ideal for what I was trying to achieve, but my efforts were futile.

I am trying to download mp3 to my file system via https://www.youtube2mp3.cc/#conversion and I am trying to execute this headless via CasperJS.

Here is my code:

var casper = require('casper').create({verbose: true , logLevel: "debug" });
var fs = require('fs');
casper.start('http://www.video2mp3.de/');
casper.waitForSelector("#converter > form");
casper.fill('#converter > form', { video: 'https://www.youtube.com/watch?v=VoaUYcwEpSw' }, true);
casper.waitForSelector("#file");
var url = casper.getElementAttribute('#file','href');
var mp3 = fs.absolute("unstoppable.mp3");
casper.then(function() { this.download(url, mp3); });
casper.run();

I think I'm a little naive, thinking that it will be so simple, but I can not find my mistakes. The debugger didn't help much. I ran my file with the following command in my terminal:

casperjs --web-security=no sample.js

Any help would be appreciated.

+4
source share
1 answer

:

, . , - :

var casper = require('casper').create({verbose: true , logLevel: "debug" });
var fs = require('fs');
var url = "https://www.youtube2mp3.cc/"
casper.start(url);
casper.then(function(){
    this.fill('#converter > form', { 'video': 'https://www.youtube.com/watch?v=VoaUYcwEpSw' }, true);
});
casper.wait(2000);//this is probably what I was missing, 
                //a screen capture showed that I wasn't waiting long enough
casper.then(function(){
    this.waitForSelector("#file", function(){
        var url = casper.getElementAttribute('#file','href');
        var mp3 = fs.absolute("unstoppable.mp3");
        casper.then(function() { this.download( url, mp3); });
    });
});
casper.run();

script. :

  • CasperJS 1.1.2
  • PhantomJS 2.1.1

MacOSX El Capitan 10.11.6 (15G1108)

+2

Source: https://habr.com/ru/post/1664361/


All Articles