我有一个简单的目标:使用phantom.js(开箱即用)或casper.js(更好,更容易)加载网页,但使用代理并从列表中旋转它,如果当前的一个是坏的(即网页加载失败或类似的东西).
我知道casper.js有--proxyparam但是它指示用户只指定一个代理并在运行时使用它.
问题#1是:如何以编程方式动态旋转代理?
我做了一些研究,发现了这个节点请求者,但它没有与之集成casper.js.我试图在代码中提取出代理功能但是并没有真正理解它是如何工作的(我猜我不是那么聪明).
所以问题#2:是否有一些简单的代理轮换实现,可以使用phantom.js或者casper.js?
我喜欢使用这种花哨casper.js但是也会phantom.js裸露下来.
我正在尝试使用CasperJS从twitter获取一些信息.我被无限卷轴所困扰.问题是即使使用jquery滚动页面也没什么作用.既不滚动也不触发确切的事件window(像uiNearTheBottom一样)似乎没有帮助.有趣的事情 - 所有这些尝试都可以通过FF和Chrome中的js控制台注入JS代码.这是示例代码:
casper.thenEvaluate(function(){
$(window).trigger('uiNearTheBottom');
});
Run Code Online (Sandbox Code Playgroud)
要么
casper.thenEvaluate(function(){
document.body.scrollTop = document.body.scrollHeight;
});
Run Code Online (Sandbox Code Playgroud) 我的页面上有html元素,这是在表单登录后的安全页面上
<h4 class="something" id="somethingelse1" style="display: none;">Some Name</h4>
<h4 class="something" id="somethingelse2" style="display: none;">Some other name</h4>
Run Code Online (Sandbox Code Playgroud)
我希望能够模拟此元素的单击,然后将加载新页面.一旦加载了新页面,我想在其中拍摄div元素的快照.我有一部分拍摄快照,但我无法找到基于其文本点击元素的方法.例如,模拟上面的H4元素的点击,其中文本是'其他名称'
这就是我到目前为止......
var casper = require('casper').create({
pageSettings: {
loadImages: true,
loadPlugins: true
}
});
casper.start('http://localhost/', function() {
console.log("page loaded");
this.fill('form#login-form', {
username: 'lbguy',
password: 'lbguypass'
}, true);
});
casper.thenOpen('http://localhost/MysecureSite/page1', function() {
this.echo(this.getTitle());
var query_xpath = '//*[@innerHtml="Some other name"]';
require('utils').dump(this.getElementAttribute(query_xpath , 'id'));
//casper.start('http://localhost/MysecureSite/page2', function() {
// this.captureSelector('pic.png', '#someelement');
//});
});
Run Code Online (Sandbox Code Playgroud)
在页面标题之后,控制台只输出"Null".我不确定这是否也是正确的方法.目标是基于我将以编程方式发送的选定文本来模拟H4元素的单击,然后在该单击操作加载名为someelement的div的第2页之后在第2页上创建快照.
UPDATE
页面加载后,h4元素通过jquery动态添加到页面中.
谢谢
当我在CasperJS中使用download()时,我得到一个保存在系统中的文件,但该文件不包含网页的实际源代码.它只包含指向远程页面的链接.如何使用CasperJs将网页的源代码转储到本地文件中?getHTML()也只是将内容回显到终端上.如何将内容保存到文件?
我想要一个启动页面的CasperJS脚本,然后无限期地保持打开状态,这样我就可以随时通过HTTP向它发送命令.问题是整个CasperJS进程一旦casper.run调用结束就会关闭.我尝试通过wait在一个步骤中使用长调用来解决这个问题,但随后Web服务器无法对Casper执行任何操作,因为Casper正忙着等待wait调用完成.
var port = 6100;
var casper = require("casper").create();
casper.start("http://google.com");
casper.then(function() {
// If I include this wait, then the server works, but the
// `this.capture` call inside it never executes, presumably
// because casper is executing the `then` calls in serial.
// But if I don't include this wait, the entire process finishes
// instantly and I can't use the server anyway.
this.wait(100000000, function() {
console.log("i have waited");
});
});
casper.run(function() {
console.log("finished"); …Run Code Online (Sandbox Code Playgroud) 我使用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,所以如果这是唯一的方法请给我一些例子.
我想使用CapserJS加载页面,但是如何发送从该页面的chrome的http请求标头导出的cookie?
如:
"SUB = _2AkMjHt3gf8NhqwJRmPkQzG_qZIp_yA3EiebDAHzsJxJTHmMJ7IUyLkMN2K7WzRJvm-Tv3YY0xyZo; SUBP = 0033WrSXqPxfM72-Ws9jqgMF55529P9D9WhCT_2hbJ1W1Cc4xfF-mFPo;"
then在使用CasperJS时,有多种方法是很常见的.以下是一个例子:
casper.then(function(){
var a = "test";
// ...
})
casper.then(function(){
// how to use the variable a in the first "then"
})
Run Code Online (Sandbox Code Playgroud)
我的问题是,将值从前thens 传递到后续thens 的常用方法是什么?对于前面提到的例子,如何a在第二个使用then?
我一直在寻找在osx中进行无头测试的解决方案.但我需要能够保存服务器返回的文件.
我测试了selenium,phantomjs,casperjs,并研究了我能在网上找到的任何东西.
他们都不支持下载.我错过了什么吗?有没有支持下载的无头浏览器/测试框架?
我正在尝试使用CasperJS构建功能测试. caseperjs由后端测试套件使用以下命令运行:
PHANTOMJS_EXECUTABLE=../client/node_modules/phantomjs/bin/phantomjs ../client/ext_modules/casperjs/bin/casperjs test ../client/test/functional/init.coffee
Run Code Online (Sandbox Code Playgroud)
在init.coffee中,我想导入/包含其旁边就位的其他模块(文件).怎么做?
以下不起作用:
require("user")
Run Code Online (Sandbox Code Playgroud)
我想要的是将其他文件中的内容导入init.coffee
casperjs ×10
javascript ×7
phantomjs ×6
node.js ×2
coffeescript ×1
cookies ×1
jquery ×1
proxy-server ×1
selenium ×1
web-scraping ×1