Switch to iframe with phantom.js

I would like to switch to iframe using pure phantom.js code

Here is my first attempt

var page = new WebPage();
var url = 'http://www.theurltofectch'
page.open(url, function (status) {
    if ('success' !== status) {
        console.log("Error");
    } else {
        page.switchToFrame("thenameoftheiframe");
        console.log(page.content);
        phantom.exit();
    }
});

It produces only the source code of the main page. Any idea?

Please note that the iframe domain is different from the domain of the main page.

+4
source share
3 answers

replace

console.log(page.content);

from

console.log(page.frameContent);

Should return the contents of the phantom frames of the frame.

If the iframe is from another domain, you may need to add the -web-security = no parameter, for example:

phantomjs --web-security=no myscript.js

, , xMythicx, . Javascript . iframe , , . , , , .

+2

, . , , , iframe . .

var page = require('webpage').create(),
    testindex = 0,
    loadInProgress = false;

page.onConsoleMessage = function(msg) {
  console.log(msg);
};

page.onLoadStarted = function() {
  loadInProgress = true;
  console.log("load started");
};

page.onLoadFinished = function() {
  loadInProgress = false;
  console.log("load finished");
};

/*
page.onNavigationRequested = function(url, type, willNavigate, main) {
  console.log('Trying to navigate to: ' + url);
  console.log('Caused by: ' + type);
  console.log('Will actually navigate: ' + willNavigate);
  console.log('Sent from the page\ main frame: ' + main);
};
*/


/*
  The steps array represents a finite set of steps in order to perform the unit test
*/

var steps = [
  function() {
    //Load Login Page
    page.open("https://www.yourpage.com");
  },
  function() {
    //access your iframe here
    page.evaluate(function() {


    });
  }, 
  function() {
   //any other step you want 
    page.evaluate(function() {



    });
  }, 
  function() {
    // Output content of page to stdout after form has been submitted
    page.evaluate(function() {
      //console.log(document.querySelectorAll('html')[0].outerHTML);
    });

    //render a test image to see if login passed
    page.render('test.png');

  }
];


interval = setInterval(function() {
  if (!loadInProgress && typeof steps[testindex] === "function") {
    console.log("step " + (testindex + 1));
    steps[testindex]();
    testindex++;
  }
  if (typeof steps[testindex] !== "function") {
    console.log("test complete!");
    phantom.exit();
  }
}, 50);
+1

Had the same problem for iframes and

phantomjs --web-security = no

helped in my case:]

+1
source

All Articles