首页 > 解决方案 > 如何滚动查看网站上的图像?

问题描述

所以我有这个程序,它旨在向下滚动,直到它可以找到屏幕上的所有图片。这是我的代码:

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

它一直向下滚动并且即使图像在屏幕上也不会停止,图片是正确的。

标签: pythonpyautogui

解决方案


我认为您需要为要查找的每个内容设置一个标志。现在编写代码的方式,allsat=True只有在所有图片同时在视图中时才会执行。

这是一种粗略的方法:

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

还值得注意的是,如果您升级了pyautogui. 从文档

注意:从 0.9.41 版开始,如果定位函数找不到提供的图像,它们将引发 ImageNotFoundException 而不是返回 None。


推荐阅读