0

I'm writing a script to fill a monthly tax form using pyautogui.

There are 2 images to be clicked on.

Issue

I'm getting the following error on the second one:

PS C:\Users\Rodrigo\OneDrive\Documentos\Python> & C:/Users/Rodrigo/AppData/Local/Programs/Python/Python310/python.exe c:/Users/Rodrigo/OneDrive/Documentos/Python/SIFERE/saf.py
Traceback (most recent call last):
  File "c:\Users\Rodrigo\OneDrive\Documentos\Python\SIFERE\saf.py", line 16, in <module>
    pyautogui.click('SIFERE/agrega_saf.png')
  File "C:\Users\Rodrigo\AppData\Local\Programs\Python\Python310\lib\site-packages\pyautogui\__init__.py", line 598, in wrapper
    returnVal = wrappedFunction(*args, **kwargs)
  File "C:\Users\Rodrigo\AppData\Local\Programs\Python\Python310\lib\site-packages\pyautogui\__init__.py", line 980, in click  
    x, y = _normalizeXYArgs(x, y)
TypeError: cannot unpack non-iterable NoneType object
PS C:\Users\Rodrigo\OneDrive\Documentos\Python> 

Code

import pyautogui, time

time.sleep(3)
anio='2015' 
mes='abril'
secuencia='1'
monto='1234.56'

pyautogui.click('SIFERE/periodo.png')
pyautogui.press('tab')  
pyautogui.write(anio)
pyautogui.press('tab')  
pyautogui.write(mes)
pyautogui.press('tab')  
pyautogui.write(secuencia)
pyautogui.press('tab')  
pyautogui.write(monto)
pyautogui.click('SIFERE/agrega_saf.png')

I've copied that exact same line to another py file and it works, but not on this one.

hc_dev
  • 8,389
  • 1
  • 26
  • 38
  • Have a look to [similar question](https://stackoverflow.com/questions/70442076/pyautogui-typeerror-cannot-unpack-non-iterable-nonetype-object), does it help? – hc_dev May 21 '22 at 19:39

1 Answers1

0

I think you want to locate the given images on your screen.

Therefor PyAutoGUI has the function locateOnScreen(image) which returns a location. If the image was found, the location is defined otherwise it's None.

See also: How to detect an image and click it with pyautogui?

Try to locate the given images first. If they were found, then continue with your input.

Code

import pyautogui, time

anio='2015' 
mes='abril'
secuencia='1'
monto='1234.56'


def find_image_or_exit(image):
    i = 1
    while location == None and i < 3:  # try max 3 times (for 9 seconds)
        time.sleep(3)  # wait 3 seconds
        location = pyautogui.locateOnScreen(image)
        i += 1
    if location == None:
        print(f"Sorry! Image {image} not found on screen. Aborting!")
        exit()
    return location
    

def enter_period():
    location = find_image_or_exit('SIFERE/periodo.png')
    pyautogui.click(location)

    pyautogui.press('tab')  
    pyautogui.write(anio)
    pyautogui.press('tab')  
    pyautogui.write(mes)
    pyautogui.press('tab')
    pyautogui.write(secuencia)
    pyautogui.press('tab')  
    pyautogui.write(monto)


def add_saf():
    location = find_image_or_exit('SIFERE/agrega_saf.png')
    pyautogui.click(location)


# main steps
enter_period()
add_saf()
hc_dev
  • 8,389
  • 1
  • 26
  • 38