admin管理员组文章数量:1392050
I am using CapserJS 1.1.0-beta3 bined with PhantomJS 1.8.2.
I call an url which respond with a redirect (HTTP 302). PhantomJS automatically follows the redirect, but in my usecase PhantomJS should not follow the redirect.
The debug output of the redirect looks like:
[debug] [phantom] Navigation requested: url=.jsp, type=Other, willNavigate=true, isMainFrame=true
How can I configure PhantomJS/CapserJS to not follow redirects?
I am using CapserJS 1.1.0-beta3 bined with PhantomJS 1.8.2.
I call an url which respond with a redirect (HTTP 302). PhantomJS automatically follows the redirect, but in my usecase PhantomJS should not follow the redirect.
The debug output of the redirect looks like:
[debug] [phantom] Navigation requested: url=https://foo./bar.jsp, type=Other, willNavigate=true, isMainFrame=true
How can I configure PhantomJS/CapserJS to not follow redirects?
Share Improve this question edited Nov 20, 2014 at 11:02 Artjom B. 62k26 gold badges135 silver badges230 bronze badges asked Nov 19, 2014 at 15:54 LavezziLavezzi 851 silver badge7 bronze badges 01 Answer
Reset to default 7There is a little workaround necessary. So you need to first identify which URL is the redirect. With resource.received
you receive the response to the first request which contains the URL where it should be redirected to. But we can't do anything from this event handler. We therefore save the target URL which is identified as a redirect target for later.
Now the underlying headless browser (PhantomJS or SlimerJS) follows the redirect by requesting the new resource, but now resource.requested
provides us with the tools to abort the request (sadly this is not documented in CasperJS). So the final script looks like this:
var casper = require("casper").create();
var redirectURLs = [],
doLog = true;
casper.on("resource.requested", function(requestData, networkRequest){
if (doLog) console.log('Request (#' + requestData.id + '): ' + JSON.stringify(requestData) + "\n");
if (redirectURLs.indexOf(requestData.url) !== -1) {
// this is a redirect url
networkRequest.abort();
}
});
casper.on("resource.received", function(response){
if (doLog) console.log('Response (#' + response.id + ', stage "' + response.stage + '"): ' + JSON.stringify(response) + "\n");
if (response.status === 301) { // use your status here
redirectURLs.push(response.redirectURL);
}
});
casper.start("https://stackoverflow./q/27021176").run(function(){
this.echo("DONE");
this.exit();
});
This is adapted from my answer A: How to configure Poltergeist or PhantomJS to not follow redirects?
You could do the same as with the linked PhantomJS version directly in CasperJS by exchanging page
for casper.page
, but CasperJS has a few advantages. You can add multiple handlers to the same events with the casper.on
notation and most all you can decide if all resources are treated the same way or just page loads. So you can exchange resource.received
for page.resource.received
and resource.requested
for page.resource.requested
.
本文标签: javascriptHow to prevent redirects in CasperJSStack Overflow
版权声明:本文标题:javascript - How to prevent redirects in CasperJS? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744782824a2624817.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论