Scrolling an element on a web page to take screenshots

梦想与她 提交于 2019-12-11 10:23:29

问题


I want to automate scrolling and taking screenshots of a mobile site. The page itself is not scrollable, so $oIE.document.parentwindow.scroll doesn't work. I don't know how to scroll an element on that page (which contains the content I want to take screenshots of).

$oIE = _IECreate($page_likers_url)
While Not (IsObj($oIE))
    Sleep(1000)
    $oIE = _IECreate($page_likers_url)
WEnd
While $oIE.ReadyState <> 4
    Sleep(1000)
WEnd

$LastHeight = $oIE.document.body.scrollHeight
$Flag = True
$Times = 0

While $Flag And $Times < $MaxScrollTimes
    $oIE.document.parentwindow.scroll(0, $oIE.document.body.scrollHeight)
    Sleep(1000)
    If $oIE.document.body.scrollHeight == $LastHeight Then
        For $i = 0 To 29
            $oIE.document.parentwindow.scroll(0, $oIE.document.body.scrollHeight)
            Sleep(1000)
        Next
        $Times += 10
        If $oIE.document.body.scrollHeight == $LastHeight Then $Flag = False
    EndIf
    $LastHeight = $oIE.document.body.scrollHeight
    $Times += 1
WEnd

This is what I normally use, but it does not work on scrollable elements (other than the page itself).


回答1:


… I don't know how to let it scroll the middle section of the page.

Seems a <div> -element scroll-able by JavaScript code. Possibly by targeting contained <li> -elements; example:

#include <IE.au3>

Global Const $g_iDelay    = 1000 * 2
Global Const $g_sURL      = 'm.benlai.com'
Global Const $g_sIdFrame  = 'slider'
Global Const $g_sNameItem = 'li'

Global       $oIE         = _IECreate($g_sURL)
Global       $oFrame      = _IEGetObjById($oIE, $g_sIdFrame)
Global       $oItems      = _IETagNameGetCollection($oFrame, $g_sNameItem)

For $oItem In $oItems

    _IEAction($oItem, 'scrollintoview')
    Sleep($g_iDelay)

Next

_IEQuit($oIE)

Or $oItem.scrollIntoView. May require repetition (if extra elements load on scrolling).

This is the code I normally use …

There is no differentiation for = -use between assignment and comparison (as per Documentation - Language Reference - Operators):

==

Tests if two strings are equal. Case sensitive. The left and right values are converted to strings if they are not strings already. This operator should only be used if string comparisons need to be case sensitive.



来源:https://stackoverflow.com/questions/48876246/scrolling-an-element-on-a-web-page-to-take-screenshots

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!