Gale
Gale

Reputation: 399

Enable/disable javascript inside casperjs step?

I'm using casper.open to load url with some post data.

I need to parse the html to get user ID from html and insert js code that sets window.name with that ID before html evaluate.

I can't do it after url loads because it is redirected to anorher url (by js) if window.name with ID is not set.

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

UPDATE:

I have succeed to get page html before js redirect by disabling js with casper.options.pageSettings.javascriptEnabled = false; placing it before casper.start(), but then js stay disabled in every other step.

Can i enable/disable js inside step?

This is code i have:

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

});

From phantomjs documentation:

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

Upvotes: 1

Views: 2809

Answers (2)

Tarun Sahani
Tarun Sahani

Reputation: 1

If you want to remove disabled attribute from button then try this

document.querySelector('#btnLogin').removeAttribute('disabled');

Upvotes: 0

Artjom B.
Artjom B.

Reputation: 61892

You can hook to an event that denotes the initialization of the page (wrapper around the PhantomJS event):

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

casper.start(url).run();

If you really need access to the page, then you could register to the "page.resource.requested" event and load the page with casper.open. With this event, you can abort requests such as the redirect request. Since the redirect happens to the same URL, you will have to find out a different way of distinguishing the first request and the second request to that URL. Example:

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?

No, you need JavaScript enabled to access and change the DOM. You can however disable the JavaScript, load the page, change it (through replace), re-enable JavaScript and load the page from the changed string.

Example:

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

Upvotes: 4

Related Questions