Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/362.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/drupal/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何等待pyautogui.locateonscreen()返回值?_Python_Pyautogui - Fatal编程技术网

Python 如何等待pyautogui.locateonscreen()返回值?

Python 如何等待pyautogui.locateonscreen()返回值?,python,pyautogui,Python,Pyautogui,我正在创建一个webbot,我想等待某个图像出现在网页上,然后再继续我的脚本 我正在使用pyautogui.locatesonscreen()函数,但在对象出现之前,我找不到一种方法来定位它 我的脚本似乎只是在返回值none之前运行locateonscreen函数一次-我希望它一直循环,直到找到图像。您可以循环脚本: import pyautogui image = pyautogui.locateOnScreen("image.png") #Searches for the image wh

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

我正在使用pyautogui.locatesonscreen()函数,但在对象出现之前,我找不到一种方法来定位它


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

您可以循环脚本:

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)

您可以循环脚本:

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)

我知道这篇文章已经有两年了,但我发现这篇文章提出了同样的问题,并且提供的解决方案不适用于pyautogui v0.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)
发件人:
注意:从版本0.9.41开始,如果定位函数找不到提供的映像,它们将引发ImageNotFoundException而不是返回None。

我知道这篇文章已经有两年了,但我发现它提出了相同的问题,并且提供的解决方案不适用于pyautogui v0.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)
发件人: 注意:从版本0.9.41开始,如果定位函数找不到提供的映像,它们将引发ImageNotFoundException,而不是返回None