Question: Here's the code that I'm trying to run:

```
import pyautogui
r=pyautogui.locateOnScreen('C:\Users\David\Desktop\index.png',grayscale=False)
print r

```

Answer: 
The official [documentation](https://pyautogui.readthedocs.io/en/latest/screenshot.html) says;

```
The Locate Functions
NOTE: As of version 0.9.41, if the locate functions can’t find the provided image, 
they’ll raise ImageNotFoundException instead of returning None.

```

So you can decide whether an exception was raise or not. Also you should try for a finite number of times not a `While True` loop.

```
retry_counter = 0
while retry_counter < 5:
    try:
        result = pyautogui.locateOnScreen(IMAGE_PATH_TO_FIND)
        if result:
            time.sleep(1)
            retry_counter = 10  # to break the loop
    except:
        time.sleep(1)  # retry after some time, i.e. 1 sec
        retry_counter += 1

```
If you have taken screenshot with snipping tool it wont work, so take screenshot with "prt sc" or command prompt. This worked for me!