我该怎么等pyautogui.locateon屏幕()返回值?

2024-10-03 23:18:59 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在创建一个webbot,我想等待某个图像出现在网页上,然后我继续我的脚本。在

我在利用pyautogui.locatesonscreen()函数,但在对象出现之前,我无法找到继续定位该对象的方法。在

我的脚本似乎只运行了locateonscreen函数一次,然后返回一个none值—我希望它在找到图像之前一直循环。在


Tags: 对象方法函数定位图像脚本none网页
2条回答

我知道这篇文章已经发布两年了,但是我发现这篇文章提出了同样的问题,而且提供的解决方案不适用于pyautoguiv0.9.41及更高版本。下面是我自己的:

import pyautogui


location = None
imageFile = 'image.png'

while (location == None):
    try:
        location = pyautogui.locateOnScreen(imageFile)
    except Exception as e:
        print(e)

print(location)

来自pyautogui.readthedocs.io: 如果找不到映像,则返回映像的版本41.0。在

可以循环脚本:

import pyautogui
image = pyautogui.locateOnScreen("image.png")

#Searches for the image
while image == None:
    image = pyautogui.locateOnScreen("image.png")
    print("still haven't found the image")

#When program finds image, print the location
print(image)

相关问题 更多 >