Reputation: 3
So what my program is supposed to do is to locate in the default apps window Mozilla Firefox and after it does so click on it and change it to Brave Browser. Although I have looked at the documentation I saw that everything in my code is as it should to be, but I keep getting this error:
File "g:\Default-Browser\main.py", line 9, in <module>
x, y = pyautogui.locateCenterOnScreen('G:\Default-Browser\mozilla.png')
TypeError: cannot unpack non-iterable NoneType object
Here is my code:
import pyautogui
from time import sleep
pyautogui.press("win")
sleep(.2)
pyautogui.typewrite("default")
sleep(.1)
pyautogui.press("enter")
x, y = pyautogui.locateCenterOnScreen('G:\Default-Browser\mozilla.png')
pyautogui.moveTo(x, y)
pyautogui.click(x, y)
sleep(.3)
x, y = pyautogui.locateCenterOnScreen(['G:\\Default-Browser\\brave.png'])
pyautogui.click(x, y)
pyautogui.hotkey('alt', 'f4')
quit()
Upvotes: 0
Views: 2599
Reputation: 159
The image isn't being detected, and consequently it is impossible for python to unpack the variable. Your code only runs the image detection once, and, when trying to automate a GUI process where loading times can vary depending on animations, computer speed, and more, it is generally not a good idea to just detect it once. Also, it isn't 100% reliable, it can take more than a try. I always, when automating a process, use a loop untill the image is detected. You could make a function like the following:
def detect_image(path, duration=0):
while True:
image_location = pyautogui.locateCenterOnScreen(path)
if image_location:
pyautogui.click(image_location[0], image_location[1], duration=duration)
break
With this, you won't get that error, and, if it takes a long time and for some reason doesn't find the image, then you should probably retake the screenshot.
On the other hand, if you think the image is going to vary every single time but slightly, you could use the confidence
parameter in the pyautogui.locateCenterOnScreen
method. Where the lower the confidence, the more likely it is to detect a more different image. However, if you set the confidence too low, it can detect false positives (images that are not the one you're actually looking for). So you should be aware that lowering the confidence can generate some problems. The code would be the following:
pyautogui.locateCenterOnScreen('image.png', confidence=0.5)
.
Keep in mind that in order to use this function you will have to install opencv, with the following pip command: pip install opencv-python
.
And lastly, if you want to remove colors from the picture, and detect an image that always remains the same, but the colors change, then you could pass the grayscale
argument to the pyautogui detection method, and that would convert the image to black and white, and then do the detection. Just like this:
pyautogui.locateCenterOnScreen('image.png', grayscale=True)
Upvotes: 1