Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

536
Views
PhantomJS : How to check for null when iterating through script tags?

I am running a PhantomJS script from node, using a child process and execFile() function. I am passing a url from my node server to the script as an argument. Sometimes the url results in a 404, and no script tags are found within the page. This results in the PhantomJS script crashing, and eventually my app. How can I take care of null values when calling document.getElementsByTagName('script')[i].innerHTML in the following code ?

"use strict";

var page = require('webpage').create();
var system = require('system');
var args = system.args;

page.open(args[1], function () {

  var scriptCtnt = page.evaluate(function() {     
    for (var i = 0; i < 20; i++) {    
        var scriptStr = [].map.call(document.getElementsByTagName('script')[i].innerHTML, function(data) {                    
           return data;                                
        }).join('');    
        if (scriptStr.indexOf('some.field') !== -1) {
          return scriptStr;
        } 
      } 
  });

  var str = scriptCtnt;
  str = str.replace('//<![CDATA[','{');
  str = str.replace(/}},/g,'}}');

  var jsonStr = JSON.stringify(JSON.parse(str));

  console.log(jsonStr);

  phantom.exit();
});

N.B : I tried to make a variable and to check for undefined values, but it didn't work. I also can't deal with this issue in my node api, because neither error nor stderr are called.

The node code calling the PhantomJS script :

      const execFile = require('child_process').execFile;
      let bin = "phantomjs" 
      let args = ['./my.service.js', urlToScrape];
      let jsonStr;

      execFile(bin, args , (error, stdout, stderr) => {

        if (error) {              
          return;
        }

        console.log(`stdout: ${stdout}`);

        if (stdout) {
          res.json(JSON.parse(stdout));              
        } else {
          res.json(stderr);
        }

      }); 

Edit :

Here is one of the ways I tried, the else returns an empty string, but it was just for the sake of testing.

 var scriptCtnt = page.evaluate(function() {     
    for (var i = 0; i < 20; i++) {
        var elem = document.getElementsByTagName('script')[i].innerHTML;
        if (typeof elem != 'undefined') {
          var scriptStr = [].map.call(elem, function(data) {                    
            return data;                                
          }).join('');    
          if (scriptStr.indexOf('some.field') !== -1) {
            return scriptStr;
          }
        } else {
          return '';
        }        
      } 
  });
over 4 years ago · Santiago Trujillo
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!