标签: casperjs

如何在CasperJS中获取当前执行的文件目录

我正在使用CasperJS检查一些站点并将JSON数据写入文件.文件应写入public/data文件夹.但是当我试图casperjs 在我的项目目录(例如我的主目录)之外调用时,它直接写入文件~/public/data,而不是在我的项目目录中.

我该怎么解决这个问题?我还没有找到如何获取__dirname或__filename.

phantomjs casperjs

13
推荐指数
2
解决办法
6162
查看次数

SlimerJS extensions.getAddons.cache.enabled

我在通过CasperJS运行SlimerJS时遇到问题,我收到下一条消息" 1414441945905 addons.repository WARN cacheEnabled:无法获取pref:extensions.getAddons.cache.enabled".

我有一个使用CasperJS和Slimer(0.9.3)作为引擎运行的nodejs scrapper.此过程由另一个进程(作为child_process.spawn)执行.此外,此过程与PM2一起安排.它运行正常,但有时抛出这个错误,它挂起,任何想法?

node.js casperjs slimerjs pm2

13
推荐指数
1
解决办法
1113
查看次数

以编程方式获取Web请求启动器

Chrome开发者工具网络选项卡有一个启动器列,可以准确显示启动网络请求的代码.

chrome dev工具的网络选项卡

我希望能够以编程方式获取网络请求启动器的信息,所以我可以用运行脚本urlrequest search string参数,它会返回在哪里与URL匹配的每个请求的细节request search string,从页面上前来url.所以给定参数www.stackoverflow.comgoogle输出可能看起来像这样(显示请求URL,行号和请求的URL):

/   19  http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js
/   4291    http://www.google-analytics.com/analytics.js
Run Code Online (Sandbox Code Playgroud)

我调查了PhantomJS,但onResourceRequested根据文档:http://phantomjs.org/api/webpage/handler/on-resource-requested,它的回调函数不提供任何启动器信息,也不提供可以派生它的上下文. HTML

是否可以使用PhantomJS或其他一些工具或服务如硒?

UPDATE

从评论和答案到目前为止,似乎Phantom,Selenium或其他任何东西目前都不支持.所以这里有一个可能有效的替代方法:加载页面和所有资产,然后request search string在所有文件中查找任何出现的内容.我怎么能这样做?

google-chrome-devtools phantomjs selenium-chromedriver casperjs selenium-webdriver

13
推荐指数
1
解决办法
1386
查看次数

casperjs在Windows机器上无法正常工作

我有一个casperjs脚本,当我在linux服务器上运行时会给出所需的结果,但是当我从笔记本电脑运行时,它不起作用.

我该如何调试?工作日志:

[info] [phantom] Starting...
[info] [phantom] Running suite: 3 steps
[debug] [phantom] opening url: http://caspertest.grsrv.com/, HTTP GET
[debug] [phantom] Navigation requested: url=http://caspertest.grsrv.com/, type=Other, willNavigate=true, isMainFrame=true
[debug] [phantom] Navigation requested: url=https://caspertest.grsrv.com/my_app, type=Other, willNavigate=true, isMainFrame=true
[debug] [phantom] Navigation requested: url=https://caspertest.grsrv.com/my_app/, type=Other, willNavigate=true, isMainFrame=true
[debug] [phantom] url changed to "https://caspertest.grsrv.com/my_app/"
[debug] [phantom] Navigation requested: url=https://caspertest.grsrv.com/my_app/#/auth, type=Other, willNavigate=true, isMainFrame=true
[debug] [phantom] url changed to "https://caspertest.grsrv.com/my_app/#/auth"
[debug] [phantom] Successfully injected Casper client-side utilities
[info] [phantom] Step anonymous 2/3 https://caspertest.grsrv.com/my_app/#/auth (HTTP 200)
[info] [remote] attempting …
Run Code Online (Sandbox Code Playgroud)

javascript web-scraping phantomjs casperjs

13
推荐指数
1
解决办法
556
查看次数

CasperJs从本地文件加载json数据

有没有方便的方法将本地JSON文件加载到CasperJs的变量中?

我看到有人建议使用

$.getJSON(filename, function() ... 
Run Code Online (Sandbox Code Playgroud)

json load file casperjs

12
推荐指数
2
解决办法
6699
查看次数

使用casperjs中的节点模块

是否可以安装通过npm安装的节点模块,然后require从casperjs脚本安装它?

(我在node.js中看到很多用于运行casper或phantom的帖子和工具,但这不是我想要做的.)

casperjs文档似乎表示这是可能的,但只能用手写的玩具模块显示,它们并没有真正做任何事情.我正在尝试安装的真实模块是imap,但是在这一点上我无法使用任何模块,甚至是内置的模块net.简单的例子:

npm install imap
echo "var test = require('imap');" > test.js
casperjs test.js
Run Code Online (Sandbox Code Playgroud)

给我:

CasperError: Can't find module imap

/usr/local/src/casperjs/bin/bootstrap.js:263 in patchedRequire
test.js:1
Run Code Online (Sandbox Code Playgroud)

(我可以看到imap模块npm ls,我可以从node.js脚本中使用它.)

或者使用内置模块:

echo "var test = require('net');" > test.js
casperjs test.js
Run Code Online (Sandbox Code Playgroud)

抱怨"找不到模块网"


我有npm --version1.4.14和nodejs --versionv0.10.29.我想知道那些太旧了吗?(Casper是1.1.0-beta,Phantom是1.9.7,两者都是最新版本.)

node.js npm phantomjs casperjs

12
推荐指数
2
解决办法
8031
查看次数

如何使用casperjs从XHR响应中捕获和处理数据?

网页上的数据是动态显示的,似乎检查html中的每个更改并提取数据是一项非常艰巨的任务,还需要我使用非常不可靠的XPath.所以我希望能够从XHR数据包中提取数据.

我希望能够从XHR数据包中提取信息,并生成要发送到服务器的"XHR"数据包.提取信息部分对我来说更重要,因为可以通过使用casperjs自动触发html元素来轻松处理信息的发送.

我附上了我的意思截图.在此输入图像描述

响应选项卡中的文本是我之后需要处理的数据.(已从服务器收到此XHR响应.)

ajax google-chrome web-scraping phantomjs casperjs

12
推荐指数
2
解决办法
1万
查看次数

如何告诉CasperJS循环浏览一系列页面

我尝试让CasperJS实现以下目标:

  • 浏览按日期顺序命名的一系列页面.
  • 在每个页面上,找到PDF链接.
  • 下载PDF.

我得到了一些工作代码,但我不明白CasperJS是如何经历事件序列的.

例如,在下面的代码示例中,CasperJS尝试处理第2步,并抛出"ReferenceError:找不到变量:formDate",而第1步由于某种原因根本不执行.

我的推理有什么问题?

在我看来,while循环以与casper.then方法不同的速度执行.

casper.start();

casper.thenOpen('http://www.example.com', function() {
    this.echo(this.getTitle());
});

casper.then(function() {

    var start = new Date('2013-01-01T00:00:00');
    var end = new Date('2013-01-31T00:00:00');

    while(start < end) {

          // step 1: define formDate  
          casper.then(function() {
            var formDate = start.getFullYear()+"-"+("0" + (start.getMonth() + 1)).slice(-2) +"-"+("0" + start.getDate()).slice(-2) ;
            casper.echo(formDate);

          });

          // Step 2: open the page and download the file
          casper.thenOpen('http://www.example.com/' + formDate, function() {

                        var url = this.getElementAttribute('div#pdffulllink a.pdf', 'href');
                        this.echo(url);
                        this.download(url, 'Downloaded_' + …
Run Code Online (Sandbox Code Playgroud)

javascript while-loop casperjs

12
推荐指数
1
解决办法
5540
查看次数

如何用es6模块组织casperjs测试

有没有办法使用ES6模块来组织CasperJS的测试,如果不是,组织测试的常规方法是什么?

phantomjs casperjs

12
推荐指数
1
解决办法
824
查看次数

如何单击"选择选项",然后使用casperjs评估加载的内容

我正在尝试抓取此产品的尺寸:

链接到产品

问题:在选择产品颜色后加载尺寸.

在产品页面的源代码中,我可以看到下拉列表中有一个onchange-method:它单击了#postColor onchange形式.

选择下拉列表:

<select name="color" id="color" class="cposelect" onchange="document.getElementById('postColor').click();" style="width:150px;margin-right: 20px; float: left;">
    <option selected="selected" onfocus="if (this.storeCurrentControl != null) storeCurrentControl(event, this);" value="0">Select colour</option>
    <option onfocus="if (this.storeCurrentControl != null) storeCurrentControl(event, this);" value="-8027">Light Camel</option>
    <option onfocus="if (this.storeCurrentControl != null) storeCurrentControl(event, this);" value="-9999">black</option>
</select>
Run Code Online (Sandbox Code Playgroud)

#postColor表单,单击onchange:

<input type="submit" name="postColor" value="" onclick="location.href=('./?model=10344-4180&amp;color='+document.forms[0].color.value+'&amp;size='+document.forms[0].size.value+'&amp;addbread=OUTLET&amp;addbread2=DRIZIA&amp;currentimage='+document.getElementById('currentimage').value+'&amp;selectedmi=a1_INDEX_14&amp;prev=10850-4314&amp;next=10413-4183'); return false;" id="postColor" class="cpobutton " style="display: none;">
Run Code Online (Sandbox Code Playgroud)

到目前为止这是我的代码,它不起作用:

casper.start('http://shop.baumundpferdgarten.com/showmodel/?model=10344-4180&addbread=OUTLET&addbread2=DRIZIA&color=0&currentimage=1&selectedmi=a1_INDEX_14', function() {
    this.test.assertExists('select[name="color"] option:nth-child(2)');
    this.click('select[name="color"] option:nth-child(2)');
    this.waitForSelector('select[name="size"] option:nth-child(2)', function() {
        this.test.pass('selector is !');
        var sizes = this.evaluate(function() {
            console.log("======== evaluating ========");
            // …
Run Code Online (Sandbox Code Playgroud)

javascript html-select phantomjs casperjs

11
推荐指数
2
解决办法
3万
查看次数