Fix pyautogui.click('button.png') so documented image-path usage works#950
Open
AbhisumatK wants to merge 1 commit intoasweigart:masterfrom
Open
Fix pyautogui.click('button.png') so documented image-path usage works#950AbhisumatK wants to merge 1 commit intoasweigart:masterfrom
AbhisumatK wants to merge 1 commit intoasweigart:masterfrom
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
The docs show click('button.png') to find an image on screen and click
its center, but this could fail with TypeError when the image wasn't
found because _normalizeXYArgs returned None and callers unpacked it.
returns None, raise ImageNotFoundException instead of returning None,
so click() and other callers never receive None.
and image not found, using a mock so the test doesn't depend on screen.
Root cause
For an image path (string), _normalizeXYArgs() calls locateOnScreen(path). When the image wasn’t found, locateOnScreen returned None (with the default pyscreeze setting), and _normalizeXYArgs returned None. Callers like click() then did
x, y = _normalizeXYArgs(x, y)and crashed with TypeError, so the documented “click by image” behavior never succeeded in the failure case and looked broken overall.Approach
Fix in _normalizeXYArgs
When firstArg is a string (image path) and locateOnScreen(firstArg) returns None, raise ImageNotFoundException instead of returning None. That way callers always get either a valid Point or a clear exception, and the documented click('button.png') flow can work when the image is found.
Cleanup
Removed the unreachable
return center(locateOnScreen(firstArg))after the except block in the string branch.Tests
Updated the _normalizeXYArgs test so that when an image path is given and the image is not found, ImageNotFoundException is expected (for both useImageNotFoundException(True) and False), and used a mock for locateOnScreen so the test doesn’t depend on screen content.
Result
pyautogui.click('button.png')works as documented: the image is located and its center is clicked.