尝试运行Phantomjs时出错

时间:2017-04-19 20:02:08

标签: javascript phantomjs casperjs

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();

更新:没有错误 - 这是正确的代码。出于某种原因,当我在终端中运行casperjs click.js时,它可以工作,而不是使用phantomjs click.js。希望这能帮助遇到此问题的其他任何人。

3 个答案:

答案 0 :(得分:0)

您的新错误可能与此问题有关:https://github.com/ariya/phantomjs/issues/14211

编辑:以下是指提交者发布但之后删除的错误。

您确定已安装了casper模块吗?

确保在" dependencies"下的package.json中指定了casper,然后在工作目录中尝试npm rebuild

如果你移动目录,你可能还想考虑使用require(' fs')。workingDirectory为你的casperPath而不是硬编码。

答案 1 :(得分:0)

您可能知道,您可以将CasperJS用于不同的目的,例如简单的浏览器自动化,功能测试或网络抓取。

根据您的目的,CasperJS脚本的结构可能会有所不同,以及您必须在终端中运行的命令。

例如,如果您创建测试脚本,它应如下所示:

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();
    });
});

要调用此脚本,必须使用casperjs test script.js。如果您使用casperjs script.js,则无效。

答案 2 :(得分:0)

我在尝试复制您的问题时遇到此错误。可能的原因是: -

1)require('casper').create()没有创建所需的对象。请检查是否已安装casperjs并且路径是否正确。

2)检查系统中安装的chrome或其他浏览器版本

我在窗户上。我已将版本更改为57.0.2987.98。转到chrome安装目录并检查版本。在Windows上就是这样的。

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

我猜你没有使用Windows。你可能需要在mac上做类似的事情。

我在Windows上的工作版本是这样的。

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');

运行代码: -

phantomjs yourfilename.js
相关问题