简体   繁体   English

如何在casper.js中更改全局变量

[英]how to change a global variable inside in casper.js

Title looks pretty straight forwoard but I am having difficulties with it. 标题看起来挺直的,但是我遇到了困难。

casper.then(function () {
        var tempname = casper.fetchText(x('//*[@id="root"]/div[4]/div/div/div[1]/div/div/div[2]/div/div[2]/div/h1'));

    oc[a]=tempname;
    console.log(" this works: "+oc[a]); //oc array vanishes
});
console.log("*******this doesnt******** " + oc[1]);

in this code piece inside the function I think it creates another variable called oc[] but this time it is local variable. 在函数内部的这段代码中,我认为它会创建另一个名为oc []的变量,但这一次它是局部变量。 How can I change the global variable oc[] just like in c++ . 像c ++一样,如何更改全局变量oc []。 I defined var oc = [] at global scope but when I try to change global one doesnt change at all. 我在全局范围内定义了var oc = [],但是当我尝试更改全局变量时,根本没有改变。 to show you that I declared my oc array at global scope here is the code below 告诉你我在全局范围内声明了我的oc数组,这是下面的代码

// JavaScript source code
// JavaScript source code
phantom.casperPath = 'C:/casperjs';
phantom.injectJs(phantom.casperPath + '/bin/bootstrap.js');


var casper = require('casper').create({
    pageSettings: {
        loadImages: false,//The script is much faster when this field is set to false
        loadPlugins: false,
        userAgent: 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/44.0.2403.157 Safari/537.36'
    }

});

//First step is to open angellist
casper.start().thenOpen("https://angel.co/login", function () {
    console.log("angel website opened");
});


//lets fill username and pass login 
casper.then(function () {
    console.log("Login using username and password");
    this.evaluate(function () {
        document.getElementById("user_email").value = "email";
        document.getElementById("user_password").value = "pass";
        document.getElementsByName("commit")[0].click();
    });
});



//go to people page

casper.then(function () {
    casper.thenOpen('https://angel.co/people').viewport(1200,800);

});



//click to profession in this case it is data scientist
casper.then(function () {

    this.click('#data-scientist');
    casper.wait(1000);

});
var bitisik = [];
var oc = [];


//wait to load


//now lets get people names
casper.then(function () {
    var accounts = "";
    var howmanylinks=0;
    var x = require('casper').selectXPath;
    var fs = require('fs');


    var personCount = this.evaluate(function(){ return document.getElementsByClassName("dpl36 people-list frw44 row _a _jm").length;});
    var anglinks = [];

    //counts the people on the page. Default is 12 for our website
    for(var b=0; b<personCount; b++){

        anglinks[b]= this.evaluate(function(arg){

            return  document.getElementsByClassName("name")[arg].getElementsByTagName('a')[0].href;},b);


    }

    for(var a=0; a<personCount; a++) {
        casper.thenOpen(anglinks[a]).viewport(1200,800);


        (casper.then( function () {
            howmanylinks = this.evaluate(function(){
                return document.getElementsByClassName('darkest dps64 profiles-show fls45 links _a _jm')[0].getElementsByTagName('a').length;
            });
            //console.log(howmanylinks);
        }),howmanylinks);

        //everything above looks correct to me


        casper.then(function () {
            var tempname = casper.fetchText(x('//*[@id="root"]/div[4]/div/div/div[1]/div/div/div[2]/div/div[2]/div/h1'));

            oc[a]=tempname;
            console.log(" this works: "+oc[a]); //oc array vanishes
        });
        console.log("*************** " + oc[1]);


        (casper.then(function (arg) {
            for (var i = 0; i < howmanylinks; i++) {

                var accounta = this.evaluate(function(arg){ return  document.getElementsByClassName('darkest dps64 profiles-show fls45 links _a _jm')[0].getElementsByTagName('a')[arg].href;},i);
                bitisik[a] = bitisik[a] + accounta;
                console.log(accounta);
            }
        }),a);





    }

    (casper.then(function () {

    }),oc,bitisik);




});

//mandatory stuff to run the js
casper.then(function () {

    casper.exit();
});
casper.run();

Your code executes in a different order: 您的代码以不同的顺序执行:

The function passed to the then method is not immediately executed, but later, asynchronously. 传递给then方法的函数不会立即执行,而是稍后异步执行。 So oc[a] is not immediately executed either. 因此oc[a]也不会立即执行。

Your console.log(oc[1]) however, is executed immediately, well before oc[1] receives a value. 但是,您的console.log(oc[1])立即执行,远在oc[1]收到值之前。

Please also note that therefore it is also wrong to reference a in that asynchronous function, because by the time it executes, your a will already have reached its maximum value. 还请注意,因此在该异步函数中引用a也是错误的,因为在执行时,您的a将已经达到其最大值。 Your for loop will finish before the function in the then method executes for the first time. for循环将在then方法中的函数第一次执行之前完成。

So in the end, oc[1] will never get a value at all. 因此,最终, oc[1]根本不会获得值。

Note that this issue is also present with other code you have in other then functions. 请注意,这个问题也存在与你在其他有其他代码then功能。

Now, to initialise the oc[] array, pass a as an argument, for instance like this: 现在,要初始化oc[]数组,请将a作为参数传递,例如:

    function setOc(a) {
        var tempname = casper.fetchText(x('//*[@id="root"]/div[4]/div/div/div[1]/div/div/div[2]/div/div[2]/div/h1'));

        oc[a]=tempname;
        console.log(" this works: "+oc[a]); //oc array vanishes
        if (a === personCount - 1) {
            // oc is initialised, do anything you want from here.
        }
    };
    casper.then(setOc.bind(a));

Alternatively, you could put a loop on a inside the above function, and things will be easier. 另外,您可以在上述函数a内部放一个循环,这样会更容易。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM