How to work with a frame?

1,439 views
Skip to first unread message

Craig Anderson

unread,
Jul 29, 2013, 11:37:31 AM7/29/13
to phan...@googlegroups.com
I am using PhantomJs to develop a web scraping script on a dynamic JavaScript driven site.
The biggest problem I have had is working against a frame.  I wrote my own function to 
wait for an element to appear in a frame.  Is there support to wait for and script against a frame?

// in a library module file:
// deferred is a q.defer() from https://github.com/kriskowal/q
wait_for_selector_in_frame = function(page,frame_selector,element_selector,wait_time,deferred) {
  var start_time = new Date;
  var timeout_function = null;

  console.log('wait_for_selector_in_frame frame_selector ' + frame_selector + ' element_selector ' + element_selector);
  timeout_function = function() {
    var found = null;
    var time_diff = null;
    var time_now = null;

    found = page.evaluate(function(frame_selector,element_selector) {
      var element = null;
      var frame_content_window = null;
      var frame_content_window_document = null;
      var frame_element = null;
      var progress = '';

      frame_element = document.querySelector(frame_selector);
      if (frame_element) {
        progress = progress + ' ' + frame_selector;
      } else {
        progress = progress + ' ' + frame_selector + ' not found';
        return progress;
      }
      if (frame_element.tagName !== 'FRAME') {
        progress = progress + ' not a FRAME';
        return progress;
      }
      frame_content_window = frame_element.contentWindow;
      if (frame_content_window) {
        progress = progress + ' contentWindow';
      } else {
        progress = progress + ' contentWindow not found';
        return progress;
      }
      frame_content_window_document = frame_content_window.document;
      if (frame_content_window_document) {
        progress = progress + ' contentWindow.document';
      } else {
        progress = progress + ' contentWindow.document not found';
        return progress;
      }
      element = frame_content_window_document.querySelector(element_selector);
      if (element) {
        progress = 'all ' + progress + ' ' + element_selector;
        return progress;
      } else {
        progress = progress + ' ' + element_selector + ' not found';
        return progress;
      }
    },frame_selector,element_selector);
    if (found.indexOf('all') === 0) {
      console.log('found selector ' + found);
      deferred.resolve('found element in frame ' + frame_selector + ' ' + element_selector);
    } else {
      time_now = new Date;
      time_diff = time_now - start_time;
      if (time_diff > wait_time) {
        console.log('timeout waiting for selector ' + found);
        deferred.reject(new Error(found));
      } else {
        setTimeout(timeout_function,200);
      }
    }
  };
  timeout_function();
  return deferred.promise;
};

James Greene

unread,
Jul 29, 2013, 7:55:31 PM7/29/13
to phan...@googlegroups.com

This has been discussed before, please search the mailing list archive. Thanks!

Sincerely,
   James Greene

--
You received this message because you are subscribed to the Google Groups "phantomjs" group.
To unsubscribe from this group and stop receiving emails from it, send an email to phantomjs+...@googlegroups.com.
Visit this group at http://groups.google.com/group/phantomjs.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

Craig Anderson

unread,
Jul 30, 2013, 10:43:12 AM7/30/13
to phan...@googlegroups.com

On Monday, July 29, 2013 5:55:31 PM UTC-6, James Greene wrote:

This has been discussed before, please search the mailing list archive. Thanks!

Sincerely,
   James Greene


Fair enough.  I am almost done with a roll my own approach shown in the code I posted.
Here is what I turned up.  After I finish the roll my own version, I'll try out switchToFrame()
 
API reference on switchToFrame:

2 group posts:
--------------------
Is there a way to get dinamically generated content from javascript using PhantomJS?
11/14/12

For example, you can see all of the frames traversal tests here:
--------------------
new Child Pages API + frames API: I propose a change
7/10/12
--------------------
 

Safvan Ak

unread,
Mar 22, 2017, 8:19:37 AM3/22/17
to phantomjs, cr...@coot.net

I am also facing that problem . I cannot get access to the DOM elements with in  frames
Reply all
Reply to author
Forward
0 new messages