我意识到"在Heroku上运行PhantomJS"是一个相关但略有不同的问题,因为我正在尝试使用节点应用程序.
我在为节点应用程序部署casperJS(基于phantomJS)脚本时遇到问题.我已经尝试通过将PhantomJS 64位二进制文件放在我的应用程序/bin目录中来部署到Heroku ,但是当我尝试运行PhantomJS时出现以下错误:
phantomjs: error while loading shared libraries: libQtWebKit.so.4: cannot open shared object file: No such file or directory
Run Code Online (Sandbox Code Playgroud)
从我读过的内容可以通过安装QtWebKit库来解决,但是Heroku没有安装它.是否有其他我可以使用的托管服务提供商可以使用或在Heroku上安装此软件包的方法?
相关代码:
app.get('/', function(request, response) {
var sys = require('sys')
var exec = require('child_process').exec;
var child;
//works but gives error while loading shared library libqtwebkit.so.4
child = exec("phantomjs --version | tr -d '\n'", function(error, stdout, stderr) {
sys.print('stdout: ' + stdout);
sys.print('stderr: ' + stderr + '\n');
response.header('Content-Type', 'application/json');
response.send('_testcb(stdout:' + stdout + '\nstderr:' + …Run Code Online (Sandbox Code Playgroud) 我试图导航到从脚本本身创建的URL.
此示例代码不起作用(我曾预期).无法弄清楚原因:(
var casper = require('casper').create({
viewportSize:{
width:1024, height:768
},
pageSettings:{
userAgent:'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_7_2) AppleWebKit/535.11 (KHTML, like Gecko) Chrome/17.0.963.79 Safari/535.11'
},
verbose:true
});
casper.on('open', function (location) {
console.log(location + ' loaded');
});
casper.start('http://www.google.com', function() {
this.test.assertTitle('Google', 'Google homepage title is the one expected');
});
casper.mytest = '';
casper.then(function () {
casper.mytest = 'http://www.yahoo.com';
});
casper.thenOpen(casper.mytest, function() {
this.test.assertTitle('Yahoo', 'Yahoo homepage title is the one expected');
});
casper.run(function () {
casper.exit();
}
);
Run Code Online (Sandbox Code Playgroud)
结果是第二页不加载:
http://www.google.com loaded
PASS …Run Code Online (Sandbox Code Playgroud) 可以说我有一系列网址.我不想使用thenOpen函数.因为它等待加载每个以前的url并减少加载时间.
casper.each(hrefs,function(self,href){
self.thenOpen(href,function(){ });
self.then(function(){
// Selectors
});
Run Code Online (Sandbox Code Playgroud)
});
与上述方法相比,您可以使用哪些方法花费更少的钱?在db中创建多个实例存储然后获取是否有效...但这很令人头痛.并且当我同时运行同一个js文件的多个实例时,也希望你也回答一下我会遇到什么问题?
我使用以下代码获取第一个表行中的所有表格单元格.我想检查每个表格单元格的innerHTML.但是在此函数返回的对象中,只有第一个表单元格实际存在,所有其他属性都为null:
firstRow = this.evaluate(function () {
return __utils__.getElementsByXPath('//tbody/tr[1]/td');
});
utils.dump(firstRow);
Run Code Online (Sandbox Code Playgroud)
utils.dump的输出是:
[
{
"abbr": "",
"align": "",
"attributes": {...}
},
null,
null,
null
]
Run Code Online (Sandbox Code Playgroud)
我也尝试过utils .findAll,它也一样.我如何获得所有匹配的元素?
当我尝试curl www.yelp.com它需要1.1秒.但是,使用CasperJS检索页面需要一分钟!
这是正常的吗?我如何找出减慢casper/phantom的速度?我怀疑它的一些HTTP重定向,casper没有关注?
var casper = require('casper').create();
var url = 'http://www.yelp.com';
casper.start(url);
casper.then(function() {
console.log( this.getHTML() );
this.exit();
});
casper.run();
Run Code Online (Sandbox Code Playgroud)

我正在尝试测试CasperJS,并正在抓取一个网格布局,如:
|Name |Name |
|Title |Title |
|Image |Image |
|Something |Something |
|----------------------
|Name |Name |
|Title |Title |
|Image |Image |
|Something |Something |
|----------------------
Run Code Online (Sandbox Code Playgroud)
如果我没有使用CasperJS,我将检索所有包含的列表(本例中为4),然后在每个容器上运行一个方法,该方法可以检索具有所需属性的对象.
我似乎很难在CasperJS中做到这一点.首先,我尝试在casper.evaluate(function(){....})中返回DOM元素列表,但它不能返回DOM元素.
然后我尝试创建一个每个循环,它将所需的对象(4)推送到一个数组,并在Evalue中返回它,但它一直返回null.
如何在CasperJS中做这样的事情.我可以以某种方式将容器的上下文返回给方法,该方法可以将对象返回到主要求值,这可以返回对象的集合吗?
我想知道如何在我们的代码中设置cli参数,而不是将它们放在我们命令的末尾,如下所示:
casperjs casper_tor.js --proxy=127.0.0.1:9050 --proxy-type=socks5
Run Code Online (Sandbox Code Playgroud)
我已经测试了类似的东西,但它不起作用:
var casper=require('casper').create();
casper.cli.options["proxy"] = "127.0.0.1:9050";
casper.cli.options["proxy-type"] = "socks5";
...
casper.run();
Run Code Online (Sandbox Code Playgroud)
我正在努力实现是设置我的代码中的新代理和报废从whatsmyip.com我的新的IP地址来检查一切是正确的(我写的机器人,将经常改变他们的代理人).
我使用CasperJS测试框架来制作一些测试套件,因为现在差不多一个月了,但我遇到了其中一个问题.
这就是我想要做的事情:我正在浏览一个网址(第1页),我必须从另一个网址(在我们的图形浏览器上模拟一个新标签)进行另一个操作,而不必退出第一个网页(第1页).第二个网址的操作将改变我的第一个网址.希望它足够清楚:)
所以现在当我到达我的第一个网址的步骤时,我通过做一个打开第二个thenOpen(),所以它正在进行一个新的导航步骤,我正在失去当前的会话,我不能再回来了.我尝试了许多方法,例如使用历史记录,重新打开页面,使用CasperJS中的事件,我也尝试使用PhantomJS但没有成功.
这是一些伪代码,使其更清晰:
casper.test.begin("A random test suite", 0, function testSuite(test) {
casper.start(url1, function () {
casper.then(function() {
// do some action on the first url
});
casper.then(function () {
// open url2 and do some action in a new tab to not lose the session of url1
});
casper.then(function () {
// check url1 (who should be still open)
});
});
casper.run(function () {
test.done();
});
});
Run Code Online (Sandbox Code Playgroud)
我真的想用CasperJS来做这件事,但我开始认为这是不可能的,我开始寻找不同的解决方案,比如这篇文章: CasperJS,并行浏览测试框架.但我以前从未使用过node.js,所以如果这是唯一的方法请给我一些例子.
我目前正在尝试使用PhantomJS和CasperJS进行端到端测试.我遇到的是PhantomJS缺乏承诺的情况.目前我们的项目实施它们.该应用程序仅用于本机支持promises的Google Chrome.
在运行我的测试时,我收到错误: 错误:ReferenceError:找不到变量:Promise
这似乎是因为PhantomJS中当前版本的Webkit不支持promises.我意识到SlimerJS确实通过Gecko提供了这种支持,但我们的应用程序在Chrome中运行,因此我希望测试在Webkit中进行.
我一直在努力的是将一个ES6 promise polyfill注入Phantom,以便正确地进行测试.我已经使用了Casper JS的injectjs以及casper.options.clientScripts.push - 两者似乎仍然缺乏对promises问题的支持.
我注意到其他人在CasperJS的github支持中说他们已经通过polyfill工作了但是我不确定他们是如何做到的,因为没有提供例子.
我已经包含了我当前脚本的一个示例.如果有人处理了这个问题并找到了解决问题的方法,那将非常感谢.先感谢您!
casper.test.begin('Example test loading', 3, function(test) {
casper.options.clientScripts.push("node_modules/es6-promise/es6-promise.js");
casper.start('http://localhost:8080/', function() {
this.captureSelector('stuff.png', 'html');
});
casper.on("remote.message", function(msg) {
this.echo("Console: " + msg);
});
casper.on("page.error", function(msg, trace) {
this.echo("Error: " + msg);
});
casper.on("resource.error", function(resourceError) {
this.echo("ResourceError: " + JSON.stringify(resourceError, undefined, 4));
});
casper.on("page.initialized", function(page) {
page.onResourceTimeout = function(request) {
console.log('Response Timeout (#' + request.id + '): ' + JSON.stringify(request));
};
});
casper.then(function() {
test.assertTitle('Example Title', 'Example title is incorrect'); …Run Code Online (Sandbox Code Playgroud) 我有以下代码:
var casper = require("casper").create({
// verbose: true,
// logLevel: "debug",
webSecurityEnabled : false
});
var links = [];
function get_links(obj) {
return obj.evaluate(function () {
var i,
l = document.querySelectorAll("a"),
l2 = [];
for (i = 0; i < l.length; i++) {
l2[i] = l[i].href;
}
return l2
});
}
function unique(arr) {
var obj = {};
for (var i = 0; i < arr.length; i++) {
if (/http(.*)?/.test(arr[i])) {
var str = arr[i];
obj[str] = true;
}
} …Run Code Online (Sandbox Code Playgroud) casperjs ×10
javascript ×6
phantomjs ×5
node.js ×2
bots ×1
dom ×1
ecmascript-6 ×1
heroku ×1
iframe ×1
php ×1
testing ×1
web-scraping ×1