1

I am quite new to Phantomjs and am starting to getting to know how to use it. However all of the semi advanced tutorials does not cover the thing i want to use Phantomjs for.

Now my question is how would i check if a Javascript is active on the site and if it is working correcly (i.e not throwing erros in the console).

I hope someone is able to point me in the right direction or know how to do this.

5
  • I think you're looking for this developer.mozilla.org/en-US/docs/Web/HTML/Element/noscript Commented Aug 19, 2013 at 12:10
  • @ep0 doesnt look like it i need to call the phantomjs from php Commented Aug 19, 2013 at 12:12
  • what do you mean by "check if a javascript is active" ? Commented Aug 19, 2013 at 13:48
  • @fusio checking if it is loaded onLoad() like when the site is loaded is the javascript loaded aswell? Commented Aug 19, 2013 at 13:51
  • possible duplicate of Check if my javascript is loaded on a site Commented Aug 21, 2013 at 15:34

1 Answer 1

1

you can interact with the open page using the webpage.evaluate method:

var page = require('webpage').create();
page.open('http://m.bing.com', function(status) {
    var title = page.evaluate(function(s) {
        //what you do here is done in the context of the page
        //this console.log will appear in the virtual page console
        console.log("test")
        //here they are just returning the page title (tag passed as argument) 
        return document.querySelector(s).innerText;
        //you are not required to return anything
    }, 'title');
    console.log(title);
    phantom.exit(); //closes phantom, free memory!
});

in order to see the virtual page console, you have to add a onConsoleMessage callback:

page.onConsoleMessage = function(msg, lineNum, sourceId) {
    console.log('CONSOLE: ' + msg + ' (from line #' + lineNum + ' in "' + sourceId + '")');
};

EDIT: by default javascript is executed.

Sign up to request clarification or add additional context in comments.

7 Comments

instead of the title is there a way i can get a list of all javascripts being loaded?
you mean the <script> elements? inside evaluate you can do everything you can in a normal browser. how would you get all the scripts in a normal page? I guess something like document.querySelector('script') ;)
if i do this i get: TypeError: 'null' is not an object (evaluating 'document.querySelector(s).innerText') phantomjs://webpage.evaluate():6 phantomjs://webpage.evaluate():8 phantomjs://webpage.evaluate():8 null
the example I provided is working, try it yourself by copying it to a js file and running phantomjs file.js in a terminal :) that error means that document.querySelector(s)returned null, i.e. it did not find s which in this example is the page title
Array.forEach.apply(document.querySelectorAll('script'), [(function(elem){ console.log(elem.getAttribute('src'));}]) Will list all script sources included in the current page.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.