简体   繁体   中英

Error when trying to run Phantomjs

phantom.casperPath +('/Users/AustinJ/Desktop/streakscraper/node_modules/casperjs');
    phantom.injectJs = (phantom.caperPath + '/Users/AustinJ/Desktop/streakscraper/node_modules/casperjs/bin/bootstrap.js');

    var utils = require('smx-casper-utils');

    var casper = require('casper').create();

    casper.userAgent('Chrome/58.0.3029.81');


    casper.start('http://streak.espn.com/en/').viewport(1200, 1000);

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

    casper.start('http://streak.espn.com/en/entry').viewport(1200, 1000);

    casper.wait(3000, function() {
      casper.capture('test1.jpg');
      casper.click(x('//*[@id="matchupDiv"]'));
    });

    casper.wait(3000, function() {
      casper.capture('test2.jpg');

    });

    casper.run();

Update: No error - here is the correct code. For some reason when I run casperjs click.js in the terminal it works instead of using phantomjs click.js. Hope this helps anyone else who runs into this problem.

Your new error might be related to this problem: https://github.com/ariya/phantomjs/issues/14211

EDIT: Below refers to a previous error the asker posted but then removed.

Are you sure you have the casper module installed?

Make sure that casper is specified in your package.json under "dependencies", then try npm rebuild in your working directory.

You may also want to consider using require('fs').workingDirectory for your casperPath instead of hardcoding it, in case you move directories.

As you probably know, you can use CasperJS for different purposes like simple browser automation, functional testing or web scraping.

Depending on what you want to do, the structure of your CasperJS script may vary, as well as the command you have to run in your terminal.

For example, if you create a test script, it should look like this:

casper.test.begin('Test my GitHub repos', function (test) {
    casper.start('https://github.com/Badacadabra', function () {
        test.assertVisible('.pinned-repos-list');
    });

    casper.then(function () {
        this.click('a[href$="repositories"]');
    });

    casper.waitForSelector('#user-repositories-list', function () {
        test.assertVisible('.js-repo-list');
        test.assertSelectorHasText('a[href$="Vimpressionist"]', 'Vimpressionist');
    });

    casper.then(function () {
        this.click('a[href$="hello-js-world"]');
    });

    casper.waitForSelector('#js-repo-pjax-container', function () {
        test.assertVisible('a[href$="LICENSE"]');
        test.assertUrlMatch(/^.*github.*$/);
        test.assertDoesntExist('.xyz');
        test.assertSelectorHasText('article', 'hello-js-world');
    });

    casper.run(function() {
        test.done();
    });
});

To call this script, you must use casperjs test script.js . If you use casperjs script.js , it will not work.

I have got this error while trying to replicate your issue. Possible reasons are:-

1) require('casper').create() didn't create the required object. Please check whether the casperjs is installed and the path is correct.

2) Check what chrome or other browser version installed in your system

I am on windows. I have changed the version to 57.0.2987.98 . Go to the chrome installed directory and check the version. It is something like this on windows.

C:\Program Files (x86)\Google\Chrome\Application

I guess you are not using windows. You may need to do similar thing on mac.

My working version on windows is something like this.

phantom.casperTest = true;
console.log(require('fs').workingDirectory);
phantom.casperPath = require('fs').workingDirectory + '\\node_modules\\casperjs';
phantom.injectJs(phantom.casperPath + '\\bin\\bootstrap.js');

var utils = require('smx-casper-utils');

var casper = require('casper').create();

casper.userAgent('Chrome/57.0.2987.98');

Run the code:-

phantomjs yourfilename.js

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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