如何从PhantomJS调用一个动作,我是否已经掌握了足够的信息

时间:2014-11-04 13:34:13

标签: javascript phantomjs

考虑一个广泛使用JavaScript来显示信息的网页。我使用PhantomJS来检索显示页面的HTML源代码。

现在,有一个按钮列表打开一个包含重要数据的框:

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

我是JavaScript的新手,但我在JS源代码中确认了以下内容,我认为这是相关的行动:

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

此代码嵌套在另一个函数中:

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
        }
      }
    )
  }
),

我的问题是:我是否有足够的信息来使用PhantomJS从这些盒子中获取数据,或者我应该进一步深入挖掘?如果我有足够的信息,任何人都能指出我正确的方向得到它?

1 个答案:

答案 0 :(得分:1)

似乎没有办法从全局范围获取对somefunction的引用,因此您需要单击.foobar-action元素来触发该函数(如果我正确理解了事件注册)。< / p>

您可以使用here这样的解决方案:

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

似乎您需要等待一段时间,直到该页面准备好做其他事情。