phantomjs命令行变量

时间:2014-01-03 11:56:12

标签: javascript linux phantomjs

我正在使用linux和phantomjs来测试一些javascript

      var page = require('webpage').create();
      var fs = require('fs');
      var address = system.args[1];
      page.onConsoleMessage = function (msg) {
          console.log('Page title is ' + msg);
      };
      page.open(address, function (status) {
          //page.open("http://vedabase.net/sb/1/1/13/en", function (status) {
          //page.open("file:///home/simha/.public_html/13.htm", function (status) { 
          //page.open("http://localhost/~simha/13.htm", function (status) {   
          page.evaluate(function () {


              element2 = document.evaluate('html/body/table/following::p[1]',
                  document,
                  null,
                  XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE,
                  null);
              thisImg1 = element2.snapshotItem(0)
              console.log(thisImg1);
              thisImg1.childNodes[1].textContent = ""



              element = document.evaluate('html/body/table | //p[text()="SYNONYMS"] | //p[text()="SYNONYMS"]/following-sibling::p[following::p[text()="TRANSLATION"]] | //p[text()="PURPORT"] | //p[text()="PURPORT"]/following-sibling::p|//a[text()="Bhaktivedanta VedaBase"]|//a[text()="Śrīmad Bhāgavatam"]|//a[text()="<<<"]/parent::*|//a[text()="<<<"]/parent::*/following::p',
                  document,
                  null,
                  XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE,
                  null);
              for (var i = 0; i < element.snapshotLength; i++) {
                  var thisImg = element.snapshotItem(i);
                  thisImg.parentNode.removeChild(thisImg);
              }

              console.log(document.title);
          });

      });
      page.onLoadFinished = function () {
          //page.render('googleScreenShot' + '.png');
          fs.write('phantom.html', page.content, 'w');

      }

我必须测试很多htmls。所以测试我使用了以下命令

  find . -name "13.htm" -exec phantomjs 1.js {} \;

它说

    ReferenceError: Can't find variable: system

     1.js:3

因为我有一个包含子文件夹和许多这样的htmls的目录,我想使用phantomjs来修改它们。

1 个答案:

答案 0 :(得分:5)

您必须使用system加载require模块,如下所示:

var system = require('system');

<强> API Reference System Wiki

<强> Page Loading Code

相关问题