baudehlo / node-phantom-simple

Simple bridge to phantomjs for Node
MIT License
201 stars 70 forks source link

How to navigate using this package. #106

Closed himanshu-jain16 closed 8 years ago

himanshu-jain16 commented 8 years ago

How to navigagte using this package? From a given URL, it should find out a link to another page and after loading that we should be able to scrape content from that. How can we do that here? Here is the sample code which does not work. Can someone tell me why it doesn't work :

driver.create({ path: require('phantomjs').path }, function (err, browser) {
  return browser.createPage(function (err, page) {
    return page.open(matchurl, function (err,status) {
      console.log("opened site? ", status);
      page.includeJs('http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js', function (err) {
        // jQuery Loaded. 
        // Wait for a bit for AJAX content to load on the page. Here, we are waiting 5 seconds. 
        setTimeout(function () {
          return page.evaluate(function () {
            //Get what you want from the page using jQuery. A good way is to populate an object with all the jQuery commands that you need and then return the object. 
            var inningsinfo = [];
            var scoretable = [];
            var recentovers = [];
            var bowlingtable = [];

            $('.innings-information').each(function () { inningsinfo.push($(this).html()); });
            $('.score-table').each(function () { scoretable.push($(this).html()); });
            $('.recent-overs').each(function () { recentovers.push($(this).html()); });

            var nav = $('.tab-full-scorecard')[0]; // obtain reference to the underlying DOMElement

           // click anchor link to "Modular code"
            var ev = document.createEvent("MouseEvents");
            ev.initMouseEvent("click", true, true, window, 0, 0, 0, 0, 0, false, false, false, false, 0, null);
            nav.dispatchEvent(ev);

            $('.bowling-table').each(function () { bowlingtable.push($(this).html()); });

            //console.log("inningsinfo",inningsinfo);

            return{innings_info:inningsinfo,
                    score_table:scoretable,
                    recent_overs:recentovers,
                    bowling_table:bowlingtable};
          }, function (err,result) {
            scrapedMatchData = result;
            browser.exit();
            callback();
          });
        }, 5000);
      });
      });
  });
});   
puzrin commented 8 years ago

@himanshu1691 the common answer for such questions is - write script to run from phantom-js directly, then port to this driver. Or use more high-level libraries. If you are sure your problem is caused by driver - please provide a more short example.

willin commented 7 years ago

@puzrin

/* global nextPage */
      const fib = () => {
        // eslint-disable-next-line
        page.evaluate(function () {
          return nextPage.toString();
        }, (err4, result) => {
          console.log(result);
          browser.exit();
        });
        // eslint-disable-next-line
        page.evaluate(function () {
          nextPage();
        }, () => {
          setTimeout(fib, 10000);
        });
      };
      fib();
Reewr commented 7 years ago

As I mentioned in #153, page.evaluate cannot access variables defined outside of itself, other than document, window and other browser (not node variables) global variables.