Skippy le Grand Gourou
Skippy le Grand Gourou

Reputation: 7694

How to call an action from PhantomJS, and do I have enough information already

Consider a webpage which makes extensive use of JavaScript for displaying informations. I use PhantomJS to retrieve the HTML source of the displayed page.

Now, there is a list of buttons which open a box containing important data :

<li class="foobar foobar-action blah foo" data-a="foo" data-b="bar">
</li>

I'm new to JavaScript but I identified in the JS source the following, which I believe to be the relevant action :

events:
{
    "click .foobar-action":"someFunction"
},

This code is nested in another function :

define(
  "some/path/whatever/someFunctionView",
  ["backbone","jquery","template!./templates/whatever.hbs","./LayerFunctionView","utils","timeManager","modalManager"],
  function(a,b,c,d,e,f,g)
  {
    return a.View.extend(
      {
        events:
        {
          "click .foobar-action":"someFunction"
        },
        someFunction:function(a)
        {
          var c=b(a.currentTarget), e=c.data("family");
          g.pane(
            new d(
              {
                date:this.date,
                bump:this.bump,
                familyP:e
              }
            ),
            c.parent(),
            "Some Text",
            {
              cssClass:"popin detail-popin whatever",
              offset:
              {
                x:0,
                y:0
              },
              position:"none",
              $target:c.parent(),
              minWidth:200,
              category:"layer"
            }
          )
        },
        render:function()
        {
          return this.$el.html(c(this.prepareJson())),
          this.delegateEvents(),
          this
        },
        prepareJson:function()
        {
          var a={};
          return a.blahUIData=e.serviceMapper.createUIFamiliesData(this.bump.woof),
          a.bump=this.bump,
          a.date=f.momentWithOffset(this.bump.heureD,this.bump.timezoneOffset).format(f.ft.DAY_MONTH_YEAR),
          a
        },
        setBump:function(a,b)
        {
          return this.date=a,
          this.bump=b,
          this
        }
      }
    )
  }
),

My question is : do I have enough information to get data from these boxes using PhantomJS, or should I dig further ? And if enough I have, can anybody point me in the right direction on how to get it ?

Upvotes: 0

Views: 152

Answers (1)

Artjom B.
Artjom B.

Reputation: 61892

There seems to be no way to get the reference to somefunction from global scope, so you need to click the .foobar-action element to trigger the function (if I understood the event registration correctly).

You can use a solution from here like this:

page.evaluate(function(){
    var ev = document.createEvent("MouseEvent");
    ev.initMouseEvent("click",true, true,window, null,
        0, 0, 0, 0,false, false, false, false,0, null
    );
    document.querySelector(".foobar-action").dispatchEvent(ev);
});
setTimeout(function(){
    // more code here
}, 100);

It seems that you need to wait a little until the page is ready to do something else.

Upvotes: 1

Related Questions