Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to scroll a page with phantomJs

Tags:

phantomjs

I wanna render a page that load its images only when the user scrolls the page. Just setting page.scrollPosition has no effect. I need something that change the scroll position over time.

like image 633
Andreas Köberle Avatar asked Feb 17 '23 14:02

Andreas Köberle


1 Answers

Not sure if this is the best way but it works. It evaluate a script in the page, that increase document.body.scrollTop over time and make a screenshot after a fixed time.

page.open "http://www.somePage.com", (status) ->
      setTimeout(( ->
        page.evaluate(->
          pos = 0
          scroll = ->
            pos += 250
            window.document.body.scrollTop = pos
            setTimeout(scroll, 100)

          scroll()
        )

        setTimeout((->
          page.render('bild.png')
          phantom.exit()
        ), 5000)
      ), 1000)
like image 71
Andreas Köberle Avatar answered Apr 26 '23 20:04

Andreas Köberle