简体   繁体   中英

Pyautogui Image non-iterable after working for a while, TypeError: cannot unpack non-iterable NoneType object

python 3.9.7 pyautogui 0.9.53

Having it look for images then moveRel and click but it's doesn't find the image it returns non-iterable

I have done to make sure it's finding the image

  while 1:
            time.sleep(1)
            if pyautogui.locateOnScreen('ScheduledBy.png', confidence=0.8) is not None:
                    print("Found")
            else:
                    print("Nope")

I'm able to find them just fine without false results

import pyautogui
import time
import keyboard
import random  
import win32api, win32con
def FindContact1():

    pyautogui.click(319,25)
    pyautogui.click(99,161)
    if pyautogui.locateOnScreen('ScheduledBy.png', confidence=0.8) is not None:
            pyautogui.moveTo('ScheduledBy.png')
            pyautogui.moveRel(245,20)
            time.sleep(.1)
            pyautogui.click()
            time.sleep(.75)
    if pyautogui.locateOnScreen('Email.png', confidence=0.8) is not None:
            pyautogui.moveTo('Email.png')
            pyautogui.moveRel(0,20)
            time.sleep(.2)
            pyautogui.tripleClick()
            time.sleep(.1)
            pyautogui.hotkey('ctrl', 'c')
            time.sleep(.1)
            pyautogui.click(88,25)
            time.sleep(.5)
    if pyautogui.locateOnScreen('Cced.png', confidence=0.8) is not None:
            pyautogui.moveTo('Cced.png')
            pyautogui.moveRel(-44,0)
            time.sleep(.1)
            pyautogui.click()
            pyautogui.hotkey('ctrl', 'v')

Log:

  File "C:\Users\CDoz\OneDrive\Documents\Projects\Python\One Exhibit Request\One Exhibit Request\FindSheduledBy.py", line 175, in Contacts
    FindContact1()
  File "C:\Users\CDoz\OneDrive\Documents\Projects\Python\One Exhibit Request\One Exhibit Request\FindSheduledBy.py", line 135, in FindContact1
    pyautogui.moveTo('Cced.png')
  File "C:\Users\CDoz\AppData\Roaming\Python\Python39\site-packages\pyautogui\__init__.py", line 598, in wrapper
    returnVal = wrappedFunction(*args, **kwargs)
  File "C:\Users\CDoz\AppData\Roaming\Python\Python39\site-packages\pyautogui\__init__.py", line 1280, in moveTo
    x, y = _normalizeXYArgs(x, y)
TypeError: cannot unpack non-iterable NoneType object
>>> 

I tried this solution based on other posts that looked for images, but either I didn't do it right or it didn't work

Post : Python pyautogui bot works for some time and then TypeError: cannot unpack non-iterable NoneType object Solution Post: PyAutoGui TypeError: cannot unpack non-iterable NoneType object

def FindContact1():
        ScheduledBy = pyautogui.locateOnScreen('ScheduledBy.png', confidence=0.8)
        Email = pyautogui.locateOnScreen('Email.png', confidence=0.8)
        Cced = pyautogui.locateOnScreen('Cced.png', confidence=0.8)
        pyautogui.click(319,25)
        pyautogui.click(99,161)
        if ScheduledBy is not None:
                pyautogui.moveTo('ScheduledBy.png')
                pyautogui.moveRel(245,20)
                time.sleep(.1)
                pyautogui.click()
                time.sleep(.75)
        if Email is not None:
                pyautogui.moveTo('Email.png')
                pyautogui.moveRel(0,20)
                time.sleep(.2)
                pyautogui.tripleClick()
                time.sleep(.1)
                pyautogui.hotkey('ctrl', 'c')
                time.sleep(.1)
                pyautogui.click(88,25)
                time.sleep(.5)
        if Cced is not None:
                pyautogui.moveTo('Cced.png')
                pyautogui.moveRel(-44,0)
                time.sleep(.1)
                pyautogui.click()
                pyautogui.hotkey('ctrl', 'v')

I can make any small changes to it and it will start working again and stop working after a day.

def FindContact1():
        ScheduledBy = pyautogui.locateOnScreen('ScheduledBy.png', confidence=0.8)
        Email = pyautogui.locateOnScreen('Email.png', confidence=0.8)
        Cced = pyautogui.locateOnScreen('Cced.png', confidence=0.8)
        pyautogui.click(319,25)
        pyautogui.click(99,161)
        time.sleep(1)
        if ScheduledBy is not None:
                print('Found Scheduled')
                pyautogui.moveTo('ScheduledBy.png')
                pyautogui.moveRel(245,20)
                time.sleep(.1)
                pyautogui.click()
                time.sleep(2)
                if pyautogui.locateOnScreen('Email.png', confidence=0.8) is not None:
                        print("Found Email")
                        pyautogui.moveTo('Email.png')
                        pyautogui.moveRel(0,20)
                        time.sleep(.2)
                        pyautogui.tripleClick()
                        time.sleep(.1)
                        pyautogui.hotkey('ctrl', 'c')
                        time.sleep(.1)
                        pyautogui.click(88,25)
                        time.sleep(2)
                        if pyautogui.locateOnScreen('Cced.png', confidence=0.8) is not None:
                                print("Found CCed")
                                pyautogui.moveTo('Cced.png')
                                pyautogui.moveRel(-44,0)
                                time.sleep(.1)
                                pyautogui.click()
                                pyautogui.hotkey('ctrl', 'v')
def FindContact1():
        pyautogui.click(319,25)
        pyautogui.click(99,161)
        time.sleep(1)
        ScheduledBy = pyautogui.locateCenterOnScreen('ScheduledBy.png', confidence=0.8)
        if ScheduledBy is not None:
                print("Found Scheduled")
                print(ScheduledBy[0], ScheduledBy[1])
                #print(ScheduledA[0], ScheduledA[1], ScheduledA[2], ScheduledA[3])
                pyautogui.moveTo(ScheduledBy[0], ScheduledBy[1])
                pyautogui.moveRel(245,20)
                time.sleep(.1)
                pyautogui.click()
                time.sleep(1)
                Email = pyautogui.locateCenterOnScreen('Email.png', confidence=0.8)
                if Email is not None:
                        print("Found Email")
                        print(Email[0], Email[1])
                        pyautogui.moveTo(Email[0], Email[1])
                        pyautogui.moveRel(-44,10)
                        time.sleep(.2)
                        pyautogui.tripleClick()
                        time.sleep(.1)
                        pyautogui.hotkey('ctrl', 'c')
                        time.sleep(.1)
                        pyautogui.click(88,25)
                        time.sleep(1)
                        Cced = pyautogui.locateCenterOnScreen('Cced.png', confidence=0.8)
                        if Cced is not None:
                                print("Found CCed")
                                print(Cced[0], Cced[1])
                                pyautogui.moveTo(Cced[0], Cced[1])
                                pyautogui.moveRel(-44,0)
                                time.sleep(.1)
                                pyautogui.click()
                                pyautogui.hotkey('ctrl', 'v')

This post actually had the answer I just didn't understand it.

Python pyautogui bot works for some time and then TypeError: cannot unpack non-iterable NoneType object Solution

I'll try and explain how I solved it, but I'm new to coding so correct me if I'm wrong.

The pyautogui.locateOnScreen produces an output of x,y,w,h and the pyautogui.moveTo would only accept x,y

Two solutions are

You can just use locateCenterOnScreen as it will only produce x,y

or

Define the image

ScheduledBy = pyautogui.locateCenterOnScreen('ScheduledBy.png', confidence=0.8)

You can use locateCenterOnScreen or locateOnScreen

then when you do pyautogui.moveTo(ScheduledBy[0], ScheduledBy[1])

the [0],[1] will grab the first two outputs of locateOnScreen & locateCenterOnScreen

locateOnScreen will have x,y,w,h [0],[1],[2],[3]

I hope this helps someone.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM