title: PhantomJS download using a javascript link
body: <p>I am attempting to scrape the below website:</p>
  
  <p><a href="http://www.fangraphs.com/leaders.aspx?pos=all&amp;stats=bat&amp;lg=all&amp;qual=0&amp;type=8&amp;season=2011&amp;month=0&amp;season1=2011&amp;ind=0&amp;team=0&amp;rost=0&amp;players=0">http://www.fangraphs.com/leaders.aspx?pos=all&amp;stats=bat&amp;lg=all&amp;qual=0&amp;type=8&amp;season=2011&amp;month=0&amp;season1=2011&amp;ind=0&amp;team=0&amp;rost=0&amp;players=0</a></p>
  
  <p>If you click the small button at the top-right of the table titled "export data", a javascript script runs and my browser downloads the file in .csv form.  I'd like to be able to write a PhantomJS script that can do this automatically.  Any ideas?</p>
  
  <p>The above button is coded into HTML as such:</p>
  
  <pre><code>&lt;a id="LB_cmdCSV" href="javascript:__doPostBack('LB$cmdCSV','')"&gt;Export Data&lt;/a&gt;&lt;/div&gt;
  </code></pre>
  
  <p>I also found this function in the HTML source code:</p>
  
  <pre><code>&lt;script type="text/javascript"&gt;
  //&lt;![CDATA[
  var theForm = document.forms['form1'];
  if (!theForm) {
      theForm = document.form1;
  }
  function __doPostBack(eventTarget, eventArgument) {
      if (!theForm.onsubmit || (theForm.onsubmit() != false)) {
          theForm.__EVENTTARGET.value = eventTarget;
          theForm.__EVENTARGUMENT.value = eventArgument;
          theForm.submit();
      }
  }
  //]]&gt;
  &lt;/script&gt;
  </code></pre>
  
  <p>I'm very new to PhantomJS/Javascript and could use some pointers here. I think I've found all the info I need to do this automatically (correct me if I'm wrong), but just not sure where to start on coding it. Thanks for any help.</p>
  
  <p>EDIT - This is what my script looks like right now:</p>
  
  <pre><code>var page = new WebPage();
  url = 'http://www.fangraphs.com/leaders.aspx?pos=all&amp;stats=bat&amp;lg=all&amp;qual=0&amp;type=8&amp;season=2011&amp;month=0&amp;season1=2011&amp;ind=0&amp;team=0&amp;rost=0&amp; players=0';
  
  page.open(encodeURI(url), function (status){
    if (status !== "success") {
      console.log("Unable to access website");
    } else {
        page.evaluate(function() {
          __doPostBack('LB$cmdCSV', '');
        });
      }
    phantom.exit(0);
  });
  </code></pre>