locateonscreen尝试多个图像



我尝试搜索多个图像,因为有时它们会改变,所以我制作了locateonscreen函数,经过3个图像尝试后,我得到了一个错误。

def locateAndClickSymbolNeonVar():
try:
#Locate Neon PNG
print("locate Neon.png")
sleep(1)
x,y = locateCenterOnScreen("Neon.png",region=(10,180,920,830), confidence=0.8)
print("click Neon.png",(x,y))
click(x,y)
print("sleep(20)")
sleep(20)
print("CheckLinkClick..")
CheckLinkClickFailedTrueAndCurl()
except:
try:
#Locate Neon2 PNG
print("SymbolNeon not found...")
print("locate Neon2.png")
sleep(1)
x,y = locateCenterOnScreen("Neon2.png",region=(10,180,920,830), confidence=0.8)
print("click Neon2.png",(x,y))
click(x,y)
print("sleep(20)")
sleep(20)
print("CheckLinkClick..")
CheckLinkClickFailedTrueAndCurl()
pass
except:
try:
#Locate Neon4 PNG
print("SymbolNeon2 not found...")
print("locate Neon4.png")
sleep(1)
x,y = locateCenterOnScreen("Neon4.png",region=(10,180,920,830), confidence=0.8)
print("click Neon4.png",(x,y))
click(x,y)
print("sleep(20)")
sleep(20)
print("CheckLinkClick..")
CheckLinkClickFailedTrueAndCurl()
pass
except:
try:
# Locate & click Google SEA Serp type 1
print("SymbolNeon2 not found...")
print("locate        WebsiteLink.png")
sleep(1)
# old 300,300,500,720
x,y = locateCenterOnScreen('       WebsiteLink.png',region=(10,180,920,830), confidence=0.8)
click(x,y)
print("       WebsiteLink.png clicked", x, y)
print("sleep(5)")
sleep(5)
CheckLinkClickFailedTrueAndCurl()
pass
except:
try:
# Locate & click Google SEA Serp type 2
print("       WebsiteLink not found...")
print("locate        WebsiteLinkName.png")
sleep(1)
# old 300,300,500,720
x,y = locateCenterOnScreen('       WebsiteLinkName.png',region=(10,180,920,830), confidence=0.8)
click(x,y)
print("       WebsiteLinkName.png clicked", x, y)
print("sleep(5)")
sleep(5)
CheckLinkClickFailedTrueAndCurl()
pass
except:
try:
# Locate & click Google SEA Serp type 3
print("       WebsiteLinkName not found...")
print("locate        WebsiteLinkIcon.png")
sleep(1)
# old 300,300,500,720
x,y = locateCenterOnScreen('       WebsiteLinkIcon.png',region=(10,180,920,830), confidence=0.8)
click(x,y)
print("       WebsiteLinkIcon.png clicked", x, y)
print("sleep(5)")
sleep(5)
CheckLinkClickFailedTrueAndCurl()
pass
except:
try:
# Locate & click Google SEA Serp type 4
print("       WebsiteLinkIcon not found...")
print("locate        WebsiteSecondLinkandIcon.png")
sleep(1)
# old 300,300,500,720
x,y = locateCenterOnScreen('       WebsiteSecondLinkandIcon.png',region=(10,180,920,830), confidence=0.8)
click(x,y)
print("       WebsiteSecondLinkandIcon.png clicked", x, y)
print("sleep(4)")
sleep(5)
CheckLinkClickFailedTrueAndCurl()
pass
except:
try:
#Locate Neon3 PNG
print("       WebsiteSecondLinkandIcon not found...")
print("locate Neon3.png")
sleep(1)
# old 300,300,500,720
x,y = locateCenterOnScreen('Neon3.png',region=(10,180,920,830), confidence=0.8)
click(x,y)
print("Neon3.png clicked", x, y)
print("sleep(5)")
sleep(5)
CheckLinkClickFailedTrueAndCurl()
pass
except:
try:
#Locate Neon5 PNG
print("SymbolNeon3 not found...")
print("locate Neon5.png")
sleep(1)
x,y = locateCenterOnScreen('Neon5.png',region=(10,180,920,830), confidence=0.8)
click(x,y)
print("Neon5.png clicked", x, y)
print("sleep(5)")
sleep(5)
CheckLinkClickFailedTrueAndCurl()
except:
print("Neon5.png not found")
pass

这是函数,但它只工作到type1,然后我得到不一致的制表符的使用。我不知道为什么,也许有更好的方法来搜索多图像。有人能帮帮我吗?

下面的代码将依次遍历文件夹中的每一张.png图片,如果找到了就点击它们:

import os
import pyautogui
import time

def locateAndClickSymbolNeonVar():
# folder directory of where the .png files are
directory = r"PATH_TO_FOLDER"
# iterate through every .png images in a folder (Neon.png, Neon1.png, Neon2.png ect.)
for images in os.listdir(directory):
if images.endswith(".png"):
# Locate on screen the current image
time.sleep(2)
coordinates = pyautogui.locateOnScreen(images)
# if locateOnScreen is not returning None
# (locateOnScreen will return none if nothing is found on the screen)
if coordinates is not None:
# click on x,y of the image located on the screen
pyautogui.click(coordinates)

locateAndClickSymbolNeonVar()

我得到一个异常无法读取GoogleSEAserpWebsiteLink.png,因为文件丢失,有不正确的权限,或者是一个不支持的或无效的格式Curl ExeptionErrorAccours 2023-01-10 22:37:38.637558

我不知道为什么,但是PNG文件在正确的文件夹中这是我的cod

#Locate & click one of four different serp Neon PNG´s (screenshoted from google search sequence)
def locateAndClickSymbolNeonVar():
# folder directory of where the .png files are
directory = r"scrimageNeonandSerp"
# iterate through every .png images in a folder (Neon.png, Neon1.png, Neon2.png ect.)
for images in os.listdir(directory):
if images.endswith(".png"):
# Locate on screen the current image
time.sleep(2)
coordinates = pyautogui.locateCenterOnScreen(images)
# if locateOnScreen is not returning None
# (locateOnScreen will return none if nothing is found on the screen)
if coordinates is not None:
print("found neon")
click(coordinates)
CheckLinkClickFailedTrueAndCurl()
else:
print("not found neon")
CheckLinkClickFailedTrueAndCurl()