我正在使用CasperJS检查一些站点并将JSON数据写入文件.文件应写入public/data文件夹.但是当我试图casperjs 在我的项目目录(例如我的主目录)之外调用时,它直接写入文件~/public/data,而不是在我的项目目录中.
我该怎么解决这个问题?我还没有找到如何获取__dirname或__filename.
我在通过CasperJS运行SlimerJS时遇到问题,我收到下一条消息" 1414441945905 addons.repository WARN cacheEnabled:无法获取pref:extensions.getAddons.cache.enabled".
我有一个使用CasperJS和Slimer(0.9.3)作为引擎运行的nodejs scrapper.此过程由另一个进程(作为child_process.spawn)执行.此外,此过程与PM2一起安排.它运行正常,但有时抛出这个错误,它挂起,任何想法?
Chrome开发者工具网络选项卡有一个启动器列,可以准确显示启动网络请求的代码.
我希望能够以编程方式获取网络请求启动器的信息,所以我可以用运行脚本url和request search string参数,它会返回在哪里与URL匹配的每个请求的细节request search string,从页面上前来url.所以给定参数www.stackoverflow.com和google输出可能看起来像这样(显示请求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
我有一个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) 有没有方便的方法将本地JSON文件加载到CasperJs的变量中?
我看到有人建议使用
$.getJSON(filename, function() ...
Run Code Online (Sandbox Code Playgroud) 是否可以安装通过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,两者都是最新版本.)
网页上的数据是动态显示的,似乎检查html中的每个更改并提取数据是一项非常艰巨的任务,还需要我使用非常不可靠的XPath.所以我希望能够从XHR数据包中提取数据.
我希望能够从XHR数据包中提取信息,并生成要发送到服务器的"XHR"数据包.提取信息部分对我来说更重要,因为可以通过使用casperjs自动触发html元素来轻松处理信息的发送.
我附上了我的意思截图.
响应选项卡中的文本是我之后需要处理的数据.(已从服务器收到此XHR响应.)
我尝试让CasperJS实现以下目标:
我得到了一些工作代码,但我不明白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) 有没有办法使用ES6模块来组织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&color='+document.forms[0].color.value+'&size='+document.forms[0].size.value+'&addbread=OUTLET&addbread2=DRIZIA&currentimage='+document.getElementById('currentimage').value+'&selectedmi=a1_INDEX_14&prev=10850-4314&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¤timage=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) casperjs ×10
phantomjs ×7
javascript ×3
node.js ×2
web-scraping ×2
ajax ×1
file ×1
html-select ×1
json ×1
load ×1
npm ×1
pm2 ×1
slimerjs ×1
while-loop ×1