Enable/disable javascript 里面的casperjs步骤?

Enable/disable javascript inside casperjs step?

我正在使用 casper.open 加载 url 一些 post 数据。

我需要解析 html 以从 html 获取用户 ID,并在 html 评估之前插入用该 ID 设置 window.name 的 js 代码。

我无法在 url 加载后执行此操作,因为如果未设置 ID window.name,它会被重定向到另一个 url(通过 js)。

casper.open('http://example.com', {
    method: 'post',
    data:{
        'somefield': 'somevalue',
    },
});

更新:

我通过禁用 js 并 casper.options.pageSettings.javascriptEnabled = false; 将它放在 casper.start() 之前成功地在 js 重定向之前获取了页面 html,但是 js 在其他所有步骤中都处于禁用状态。

我可以 enable/disable js inside step 吗?

这是我的代码:

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 文档:

The settings apply only during the initial call to the page.open function. Subsequent modification of the settings object will not have any impact.

您可以挂钩到表示 initialization of the page (wrapper around the PhantomJS event) 的事件:

casper.on("page.initialized", function(){
    this.evaluate(function(){
        window.name = "whatever";
    });
});

casper.start(url).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();

Or, can I disable page JavaScript in previous step and enable it in the next?

不,您需要启用 JavaScript 才能访问和更改 DOM。但是,您可以禁用 JavaScript、加载页面、更改它(通过 replace)、重新启用 JavaScript 和 load the page from the changed string

示例:

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');