在casperjs步骤中启用/禁用javascript?
我正在使用在casperjs步骤中启用/禁用javascript?,javascript,redirect,web-scraping,phantomjs,casperjs,Javascript,Redirect,Web Scraping,Phantomjs,Casperjs,我正在使用casper.open加载带有一些帖子数据的url 我需要解析html以从html中获取用户ID,并插入js代码,在html计算之前使用该ID设置window.name 我无法在加载url后执行此操作,因为如果未设置ID为的window.name,它将被重定向到另一个url(由js) casper.open('http://example.com', { method: 'post', data:{ 'somefield': 'somevalue',
casper.open
加载带有一些帖子数据的url
我需要解析html以从html中获取用户ID,并插入js代码,在html计算之前使用该ID设置window.name
我无法在加载url后执行此操作,因为如果未设置ID为的window.name,它将被重定向到另一个url(由js)
casper.open('http://example.com', {
method: 'post',
data:{
'somefield': 'somevalue',
},
});
更新:
我已经成功地在js重定向之前获取页面html,方法是使用casper.options.pageSettings.javascriptEnabled=false禁用js
将其放在casper.start()之前,但js每隔一步就会被禁用
我可以在步骤中启用/禁用js吗
这是我的代码:
casper.start().then(function () {
// some work
}).then(function () {
// Disable js
this.options.pageSettings.javascriptEnabled = false;
}).then(function () {
// POST call
casper.open('http://example.com', {
method: 'post',
data: {
'field': 'value'
}
});
}).then(function () {
// Enable js
this.options.pageSettings.javascriptEnabled = true;
}).then(function () {
var content = this.page.content;
var changedContent = content.replace("some text", "with text");
this.page.setContent(changedContent, this.getCurrentUrl());
});
来自phantomjs文档:
这些设置仅在首次调用页面时适用。打开
功能。设置对象的后续修改将不具有
任何影响
您可以挂钩到一个事件,该事件表示(围绕的包装器):
如果确实需要访问页面,则可以注册到
“page.resource.requested”
事件,并使用casper.open
加载页面。通过此事件,您可以中止重定向请求等请求。由于重定向发生在同一个URL上,因此您必须找到一种不同的方法来区分该URL的第一个请求和第二个请求。例如:
var firstUrlRequestDone = false;
var url = "some url";
casper.on("page.resource.requested", function(requestData, request) {
if (requestData.url.indexOf(url) === 0) {
if (!firstUrlRequestDone)
firstUrlRequestDone = true;
else
request.abort();
}
});
casper.start()
.thenOpen(url)
.thenEvaluate(function(){
// TODO: read DOM and change window.name
})
.thenOpen(url)
.run();
或者,我可以在上一步禁用页面JavaScript,然后在下一步启用它吗 不,您需要启用JavaScript才能访问和更改DOM。但是,您可以禁用JavaScript、加载页面、更改页面(通过
replace
)、重新启用JavaScript和
例如:
casper.options.pageSettings.javascriptEnabled = false;
var changedContent, actualURL;
casper.start(url)
.then(function(){
var content = this.page.content;
changedContent = content.replace("something", "with something");
actualURL = this.getCurrentUrl();
this.page.settings.javascriptEnabled = true;
})
.thenOpen("http://example.com") // this is a dummy page to force re-evaluation of `page.settings`
.then(function(){
this.page.setContent(changedContent, actualURL);
})
.then(function(){
// TODO: do whatever you need
})
.run();
您可以挂钩到一个事件,该事件表示(围绕的包装器):
如果确实需要访问页面,则可以注册到
“page.resource.requested”
事件,并使用casper.open
加载页面。通过此事件,您可以中止重定向请求等请求。由于重定向发生在同一个URL上,因此您必须找到一种不同的方法来区分该URL的第一个请求和第二个请求。例如:
var firstUrlRequestDone = false;
var url = "some url";
casper.on("page.resource.requested", function(requestData, request) {
if (requestData.url.indexOf(url) === 0) {
if (!firstUrlRequestDone)
firstUrlRequestDone = true;
else
request.abort();
}
});
casper.start()
.thenOpen(url)
.thenEvaluate(function(){
// TODO: read DOM and change window.name
})
.thenOpen(url)
.run();
或者,我可以在上一步禁用页面JavaScript,然后在下一步启用它吗 不,您需要启用JavaScript才能访问和更改DOM。但是,您可以禁用JavaScript、加载页面、更改页面(通过
replace
)、重新启用JavaScript和
例如:
casper.options.pageSettings.javascriptEnabled = false;
var changedContent, actualURL;
casper.start(url)
.then(function(){
var content = this.page.content;
changedContent = content.replace("something", "with something");
actualURL = this.getCurrentUrl();
this.page.settings.javascriptEnabled = true;
})
.thenOpen("http://example.com") // this is a dummy page to force re-evaluation of `page.settings`
.then(function(){
this.page.setContent(changedContent, actualURL);
})
.then(function(){
// TODO: do whatever you need
})
.run();
如果要从按钮中删除禁用的属性,请尝试此操作
document.querySelector('#btnLogin').removeAttribute('disabled');
如果要从按钮中删除禁用的属性,请尝试此操作
document.querySelector('#btnLogin').removeAttribute('disabled');
您可以通过一个重写代码必要部分的代理来传递http请求—您可以替换ID,删除重定向代码。@Vaviloff我想在casper得到响应之前重写响应,但是我想要casper/phantom解决方案当我转储casper.options.onResourceRequested时,我在页面中只看到原始请求,而没有看到java脚本post重定向。注意:原始页面和重定向页面具有相同的URL。您可以通过代理来传递http请求,该代理可以重写代码的必要部分-您可以替换ID,删除重定向代码。@Vaviloff我想在casper得到响应之前重写响应,但我希望casper/phantom Solution当我转储casper.options.onResourceRequested时,我在页面中只看到原始请求,而没有看到java脚本post重定向。注意:原始页面和重定向页面具有相同的URLLet us。让我们一起。