4

純粋な phantom.js コードを使用して iframe に切り替えたい

これが私の最初の試みです

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

メイン ページのソース コードのみを生成します。何か案が ?

iframe ドメインはメイン ページ ドメインとは異なることに注意してください。

4

3 に答える 3

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\'s 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);
于 2014-01-31T16:13:39.350 に答える
1

iframe と

phantomjs --web-security=いいえ

私の場合に役立ちました:]

于 2015-09-26T04:17:48.203 に答える