How to scroll a page with phantomJs

2019-03-31 03:52发布

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.

标签: phantomjs
1条回答
老娘就宠你
2楼-- · 2019-03-31 04:53

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)
查看更多
登录 后发表回答