nightmare的开发环境搭建和helloworld

来源:互联网 发布:文明 mac 编辑:程序博客网 时间:2024/05/17 04:59

1、安装nodejs

先去官网下载一个nodejs的安装包:https://nodejs.org/en/download/,



安装基本是一路next,最后命令行分别输入node --version,还有npm -v,查看安装是否成功




2、安装electron


先执行命令安装淘宝的npm镜像 

npm install cnpm -g --registry=http://registry.npm.taobao.org

需要等一会儿才能执行完

然后用淘宝镜像安装electron

cnpm install electron -g

完成后执行 electron -v 查看版本号

还可以直接执行electron,会打开一个默认的窗口,也可以算是一个helloworld



3、安装nightmare

项目地址:https://github.com/segmentio/nightmare

可以使用git下载下来,或者直接下载压缩包解压到想要的目录


执行cnpm install 来安装依赖,安装完成后,执行node example.js,就可以看到nightmare自带的helloworld


 example.js打开了yahoo,模拟输入github nightmare,并点击搜索按钮, 找到搜索结果对应的链接,并在后台打印了出来









var Nightmare = require('nightmare');var nightmare = Nightmare({ show: true })nightmare  .goto('http://yahoo.com')  .type('form[action*="/search"] [name=p]', 'github nightmare')  .click('form[action*="/search"] [type=submit]')  .wait('#main')  .evaluate(function () {    return document.querySelector('#main .searchCenterMiddle li a').href  })  .end()  .then(function (result) {    console.log(result)  })  .catch(function (error) {    console.error('Search failed:', error);  });







原创粉丝点击