在 Windows 上安装 CasperJS:如何正确安装?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14894311/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 10:16:44  来源:igfitidea点击:

Installing CasperJS on Windows: How to do it correctly?

windowsphantomjscasperjs

提问by user2075354

I know there is a documentation from CasperJS website about how to install CasperJS on Windows, but bear with me these guys only explained for the pros only.

我知道 CasperJS 网站上有一份关于如何在 Windows 上安装 CasperJS 的文档,但请耐心等待,这些人仅针对专业人士进行了解释。

If you are new to all this CasperJS and PhantomJS world, you dont stand a chance to understand not even spending two days trying to search the net for a better explanation.

如果您是 CasperJS 和 PhantomJS 世界的新手,即使花两天时间尝试在网上搜索更好的解释,您也没有机会理解。

I am working on a project that requires a screenshot of each website listed on my project website, looking around i found out that PhantomJS would be great for this task (website screenshot).

我正在开发一个项目,该项目需要我的项目网站上列出的每个网站的屏幕截图,环顾四周,我发现 PhantomJS 非常适合此任务(网站屏幕截图)。

I downloaded PhantomJS and and pasted its phantomjs.exefile in system32which is working well when i use cmd.exeto send command.

我下载了 PhantomJS 并粘贴了它的phantomjs.exe文件,system32当我cmd.exe用来发送命令时,该文件运行良好。

I even managed to grab screenshot with the phantomjs.exeonly. but my problem came when i noticed the the workload could be easier when these two work together (CasperJS and PhantomJS) as i can even be able to reduce the size of the screenshot when using CasperJS.

我什至设法用phantomjs.exe唯一的屏幕截图。但是当我注意到当这两个一起工作(CasperJS 和 PhantomJS)时工作量会更容易,因为我什至可以在使用 CasperJS 时减小屏幕截图的大小,我的问题就出现了。

In fact the only use that i want CasperJS for is the limiting of the shot size but since yesterday i have been trying to figure out how to make CasperJS work on Windows but with no avail.

事实上,我想要 CasperJS 的唯一用途是限制镜头大小,但从昨天开始,我一直试图弄清楚如何让 CasperJS 在 Windows 上工作,但无济于事。

I have downloaded CasperJS and tried to install it in many ways also trying to follow the documentation but nothing.

我已经下载了 CasperJS 并尝试以多种方式安装它,也尝试按照文档进行安装,但什么也没有。

I changed the CasperJS folder name from its download name to CasperJS as the documantation suggest but when i check in the cmd trying to call some commands, nothing happens.

我按照文档建议将 CasperJS 文件夹名称从其下载名称更改为 CasperJS,但是当我检查 cmd 尝试调用某些命令时,没有任何反应。

Anyway to cut the story shot can anyone help me in simple terms considering that i am a newbie to explain how CasperJS can be installed on window or if possible with PhantomJS only how can i re-size the iamge the the program produces lets say if i want a 960 to 400px.

无论如何剪切故事镜头任何人都可以简单地帮助我考虑到我是一个新手来解释如何在窗口上安装 CasperJS 或者如果可能的话只使用 PhantomJS 我如何重新调整程序生成的图像的大小让我说如果我想要 960 到 400 像素。

回答by isimmons

Poor documentation for windows. http://casperjs.org/installation.html#windows

Windows 的文档很差。http://casperjs.org/installation.html#windows

It starts off assuming you have already installed without telling you how to install.

它开始假设您已经安装而不告诉您如何安装。

So here it is if anyone else is confused about this. There is no actual install. It's just extracting zip contents to the right place.

所以这里是如果其他人对此感到困惑。没有实际安装。它只是将 zip 内容提取到正确的位置。

  1. download phantomjs for windows from the phantomjs site (it's a zip with binary inside)
  2. extract the contents to C:\phantomjs
  3. download the casperjs zip file from the casperjs website
  4. extract the contents to C:\casperjs
  5. Now you can add the following to the end your system or user PATH variable

    ;C:\phantomjs;C:\casperjs\batchbin

  6. restart cmd.exe to pick up the new path variable or logout/login if you are running Console2 or Conemu terminal emulator (they won't pick up new paths by a simple close and re-open)

  1. 从 phantomjs 站点下载适用于 windows 的 phantomjs(它是一个带有二进制文件的 zip)
  2. 将内容提取到 C:\phantomjs
  3. 从 casperjs 网站下载 casperjs zip 文件
  4. 将内容提取到 C:\casperjs
  5. 现在您可以将以下内容添加到系统或用户 PATH 变量的末尾

    ;C:\phantomjs;C:\casperjs\batchbin

  6. 如果您正在运行 Console2 或 Conemu 终端模拟器,请重新启动 cmd.exe 以获取新的路径变量或注销/登录(它们不会通过简单的关闭和重新打开来获取新路径)

Now in the docs it says to run it like this

现在在文档中它说像这样运行它

casperjs.bat myscript.js

Actually since both phantomjs.exe and casperjs.bat are now in the system PATH you can leave off the extension like this.

实际上,由于 phantomjs.exe 和 casperjs.bat 现在都在系统 PATH 中,因此您可以像这样省略扩展名。

casperjs myscript.js

And when running phantomjs.exe just run

当运行 phantomjs.exe 时只需运行

phantomjs

One more thing. It really doesn't matter where you install as long as you add that path to the system PATH. I installed to C:\usr\phantomjs and C:\usr\casperjs.

还有一件事。只要将该路径添加到系统 PATH 中,安装在哪里实际上并不重要。我安装到 C:\usr\phantomjs 和 C:\usr\casperjs。

回答by iChux

I itemize below the method that has served my needs on both my personal Windows and Ubuntu work PC. DO note that my method doesn't fiddle with PATH settings but involves a command you could save somewhere & copy and paste as needed:

我在下面列出了在我的个人 Windows 和 Ubuntu 工作 PC 上满足我需求的方法。请注意,我的方法不会摆弄 PATH 设置,而是涉及一个可以保存在某处并根据需要复制和粘贴的命令:

Step 1: Gather the prerequisites

第 1 步:收集先决条件

  • Download the casperjs and phantomjs versions you want to use
  • Make a directory to contain the things I want to list
  • Extract the downloaded phantomjs & copy its executable into the directory of step b
  • Extract casperjs and rename its folder to casperjs
  • Copy the renamed casperjsfolder to the directory of step b
  • Create and save a file config.jsonto the directory of step b
  • config.jsonshould contain phantomjs configurations as found here: http://phantomjs.org/api/command-line.html
  • 下载你要使用的 casperjs 和 phantomjs 版本
  • 创建一个目录来包含我想列出的东西
  • 解压下载的phantomjs并将其可执行文件复制到步骤b的目录中
  • 解压 casperjs 并将其文件夹重命名为casperjs
  • 将改名后的casperjs文件夹复制到步骤b的目录下
  • 创建一个文件config.json并保存到步骤 b的目录
  • config.json应包含 phantomjs 配置,如下所示:http://phantomjs.org/api/command-line.html

Step 2: Running your script

第 2 步:运行脚本

  • Whenever you want to work with a file, follow the Step 1details above
  • The next step assumes that you are in the directory created in step bof Step 1also have a file named first.js
  • On Windows: phantomjs.exe --config=config.json casperjs/bin/bootstrap.js --casper-path=casperjs --cli first.js
  • On Ubuntu: ./phantomjs --config=config.json casperjs/bin/bootstrap.js --casper-path=casperjs --cli first.js
  • 每当您想处理文件时,请按照上面的第 1 步详细信息进行操作
  • 下一步假设您在步骤 1 的步骤 b中创建的目录中也有一个名为first.js的文件
  • 在 Windows 上:phantomjs.exe --config=config.json casperjs/bin/bootstrap.js --casper-path=casperjs --cli first.js
  • 在 Ubuntu 上:./phantomjs --config=config.json casperjs/bin/bootstrap.js --casper-path=casperjs --cli first.js

Experimental config.jsand first.jsare listed below:

下面列出了实验config.jsfirst.js

config.json

配置文件

{"sslProtocol": "any", "cookiesFile": "biscuit", "maxDiskCacheSize": 1000, "diskCache": true}

{"sslProtocol": "any", "cookiesFile": "biscuit", "maxDiskCacheSize": 1000, "diskCache": true}

first.js

第一个.js

var casper = require('casper').create({
    pageSettings: {
        loadImages: false,
        loadPlugins: true,
        userAgent: 'Mozilla/5.0 (Windows; U; Windows NT 5.1; nl; rv:1.9.1.6) Gecko/20091201 Firefox/3.5.6'
    }
});
var url = "http://casperjs.org/";

casper.start(url).wait(60 * 1000 * 1, function() {
     casper.echo('1 min has passed');
     casper.capture('casperjs.png');
     casper.exit();
});

casper.run();

Addendum: download and save the details of screenshots.jsand run it as

附录:下载并保存screenshots.js的详细信息并作为

phantomjs.exe --config=config.json casperjs/bin/bootstrap.js --casper-path=casperjs --cli screenshots.js http://phantomjs.org

phantomjs.exe --config=config.json casperjs/bin/bootstrap.js --casper-path=casperjs --cli screenshots.js http://phantomjs.org

Run Tests: download & save the details of picturefill-test.jsand run it as

运行测试:下载并保存picturefill-test.js的详细信息 并将其作为

phantomjs.exe --config=config.json casperjs/bin/bootstrap.js --casper-path=casperjs test --cli picturefill-test.js

phantomjs.exe --config=config.json casperjs/bin/bootstrap.js --casper-path=casperjs test --cli picturefill-test.js

回答by Bashir Osman

As of CasperJS 1.1.0-DEV Beta 3you should use this PATH: C:\casperjs\batchbineven though the documentation found herestates you need to use C:\casperjs\bin

CasperJS 1.1.0-DEV Beta 3 开始,您应该使用此 PATH:C:\casperjs\batchbin即使此处找到的文档说明您需要使用C:\casperjs\bin

The reason to this is because C:\casperjs\batchbinincludes a .BAT which C:\casperjs\bindoes not include anything except some.jsfiles.

这样做的原因是因为C:\casperjs\batchbin包含一个 .BAT,C:\casperjs\bin它不包含除某些.js文件之外的任何内容。

回答by user2075354

after a 3days work, i managed to get it work the problem was with the path and the installation of phantom. i had made the path to a folder but the installation was pointing to the exe file all i had to do was to put the exe file into a folder phantomjs and that was it thank for your help all.

经过 3 天的工作,我设法让它工作,问题出在路径和 phantom 的安装上。我已经创建了一个文件夹的路径,但安装指向的是 exe 文件,我所要做的就是将 exe 文件放入一个文件夹 phantomjs 中,谢谢大家的帮助。

回答by pilavdzice

Many of the other answers are out of date. Since it seems like the correct install process keeps changing all the time, I suspect my answer will be out of date also in a few days, but as of March 19, 2015, this is the answer.The other answers, even highly upvoted ones, do not work, so don't waste your time. Good luck. Casperjs seems quite good.

许多其他答案已过时。由于正确的安装过程似乎一直在不断变化,我怀疑我的答案也会在几天内过时,但截至 2015 年 3 月 19 日,这就是答案。其他答案,即使是高度赞成的答案,也不起作用,所以不要浪费你的时间。祝你好运。Casperjs 看起来相当不错。

If you download PhantomJS 2.0 and casperjs 1.1-beta3 zip file and put the batchbin directory in the path and put phantom exe in that same folder, then run casperjs, on Windows 8.1 I get the error "CasperJS needs PhantomJS v1.x".

如果您下载 PhantomJS 2.0 和 casperjs 1.1-beta3 zip 文件并将 batchbin 目录放在路径中并将 phantom exe 放在同一文件夹中,然后运行 ​​casperjs,在 Windows 8.1 上我会收到错误“CasperJS 需要 PhantomJS v1.x”。

So, I got the latest casperjs direct from github, which has no batchbin directory, so I put just the C:\casperjs\bin directory in the path instead and this worked (sort of, I mean it seems to work well enough for me - although running casperjs c:\casperjs\tests\selftest.js seems to have a number of failing tests).

所以,我直接从 github 获得了最新的 casperjs,它没有 batchbin 目录,所以我只将 C:\casperjs\bin 目录放在路径中,这有效(有点,我的意思是它似乎对我来说足够好- 尽管运行 casperjs c:\casperjs\tests\selftest.js 似乎有许多失败的测试)。

So to recap, the bad news is only the very latest bleeding edge casperjs works. The good news is the install is 1,2,3 simple:

所以回顾一下,坏消息只是最新的前沿 casperjs 作品。好消息是安装很简单1,2,3:

  1. Clone latest casperjs from github into c:\casperjs.
  2. Copy phantomjs.exe (ver 2.0) to c:\casperjs\bin
  3. Add c:\casperjs\bin to your windows path
  1. 将最新的 casperjs 从 github 克隆到 c:\casperjs。
  2. 将 phantomjs.exe (ver 2.0) 复制到 c:\casperjs\bin
  3. 将 c:\casperjs\bin 添加到您的 Windows 路径

回答by avishayp

Fast forward to 2015... 5-steps win7 howto:

快进到 2015 年... 5 步 win7 操作方法:

  1. choco:PS me> iex ((new-object net.webclient).DownloadString('https://chocolatey.org/install.ps1'))
  2. git:choco install git -y
  3. phantomjs 2.0:choco install phantomjs -y
  4. casperjs source:git clone https://github.com/n1k0/casperjs -b phantomjs-2
  5. Add the location of casperjs/binto PATH
  1. 巧克力:PS me> iex ((new-object net.webclient).DownloadString('https://chocolatey.org/install.ps1'))
  2. 吉特:choco install git -y
  3. 幻影 2.0:choco install phantomjs -y
  4. casperjs 源码:git clone https://github.com/n1k0/casperjs -b phantomjs-2
  5. 将 的位置添加casperjs/bin到 PATH

Done. You can now casperjs --versionand live happily ever after.

完毕。您现在可以casperjs --version从此过上幸福的生活。

回答by Jacek Góraj

Ok guys. So I think this thread needs refreshing for 2018!

好,朋友们。所以我认为这个线程需要在 2018 年刷新!

So with npm, the effort is reduced significantly. Given that you have npminstalled, open terminal and go to your project:

因此,使用npm,工作量显着减少。鉴于您已npm安装,打开终端并转到您的项目:

cd your_project_name

cd your_project_name

Now install casperjs. Use --save-dev, --save, -gor none as needed:

现在安装casperjs。使用--save-dev--save-g或无需要:

npm install --save-dev casperjs

npm install --save-dev casperjs

Now install phantomjs. To do this, you should install phantomjs-prebuilt, because PhantomJS team changed their package name:

现在安装phantomjs。为此,您应该安装 phantomjs-prebuilt,因为 PhantomJS 团队更改了他们的包名:

npm install --save-dev phantomjs-prebuilt

npm install --save-dev phantomjs-prebuilt

Run your spec:

运行你的规范:

casperjs your_spec_name.js

casperjs your_spec_name.js

回答by Sven Bluege

Just in case you're using a notebook with dual graphic cards like I do: choose one of them to prevent issues. This article helped me out:

以防万一您像我一样使用带有双显卡的笔记本电脑:选择其中之一以防止出现问题。这篇文章帮助了我:

casperJS not finishing on windows

casperJS 没有在 Windows 上完成

the casperJS documentation is pretty clear about it but I had no clue what to do until I read the notice above.

casperJS 文档对此非常清楚,但在阅读上面的通知之前,我不知道该怎么做。