How to pass a variable as an argument to a CasperJS script through the command line?

前端 未结 5 819
挽巷
挽巷 2020-12-10 11:38

I\'m using PhantomJs, CasperJs, and Js in a js file ran through the cmd.

Imagine we had two files(test1.js, and test2.js). Both files have a url/site variable that d

5条回答
  •  温柔的废话
    2020-12-10 12:09

    You could have one file with your global variables, then call them in the other files. Like that when you want to modify one variable, you have only one file to modify. Use phantom.injectJs(path/to/file) to call other files in your main files. It Works with phantomJS and slimerJS.

    Example :

    js_file

    --variable.js--
    var site='http://google.com';
    

    js_file

    --file1.js--
    phantom.injectJs( 'variable.js');
    casper.start(site, function(){
        ...
    });
    

    js_file

    --file2.js--
    phantom.injectJs( 'variable.js');
    casper.thenOpen(site, function(){
        ...
    });
    

提交回复
热议问题