Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to navigate using this package. #106

Closed
himanshu-jain16 opened this issue Jan 12, 2016 · 3 comments
Closed

How to navigate using this package. #106

himanshu-jain16 opened this issue Jan 12, 2016 · 3 comments

Comments

@himanshu-jain16
Copy link

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
Copy link
Contributor

puzrin commented Jan 16, 2016

@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.

@puzrin puzrin closed this as completed Jan 29, 2016
@willin
Copy link

willin commented Aug 7, 2017

@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
Copy link

Reewr commented Aug 8, 2017

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants