Search code examples
pythonpyautogui

How to scroll to see images on a website?


So I have this program that is meant to scroll down until it can find all of the pictures on the screen. This is my code:

def scrolluntil():
    allsat = False
    while allsat == False:
        pyautogui.scroll(-100)
        fan = locateCenterOnScreen("findaname.png")
        tl = locateCenterOnScreen("topleft.png")
        tr = locateCenterOnScreen("topright.png")
        if fan is not None:
            if tl is not None:
                if tr is not None:
                    allsat = True

It keeps scrolling down and doesn't stop even when the images are on the screen, the pictures are correct.


Solution

  • I think you need to set a flag for each of the things you want to find. The way the code is written now, allsat=True will only execute when all pictures are simultaneously in view.

    This is a crude way to do it:

    def scrolluntil():
        allsat = False
        fan    = False
        tl     = False
        tr     = False
        while allsat == False:
            pyautogui.scroll(-100)
            if fan == False:
                if locateCenterOnScreen("findaname.png") is not None:
                    fan = True
            if tl == False:
                if locateCenterOnScreen("topleft.png") is not None:
                    tl = True
            if tr == False:
                if locateCenterOnScreen("topright.png") is not None:
                    tr = True
            if fan:
                if tl:
                    if tr:
                        allsat = True
    

    It's also worth noting that you may have to update your code to use a try...except block if you ever upgrade your version of pyautogui. From the docs:

    NOTE: As of version 0.9.41, if the locate functions can’t find the provided image, they’ll raise ImageNotFoundException instead of returning None.