Im facing some problems when i run my code. Im developing the same code in Mac and in Windows.
The function LocateOnScreen from PyAutoGui is not working in Windows, even I installed OpenCV. The same image in MAC works perfetctly
I created a funtion to know if the code is running in Windows or Mac, just because the resolution
See the code below
cond = True
while cond:
try:
x, y = auto.center(auto.locateOnScreen(settings.btnLogin, confidence=.5))
## Click in Windows
auto.click(x, y)
## Click in Mac
auto.click(x/2, y/2)
cond = False
except Exception as e:
print("Error", e)
return
I just cant understand why it works in Mac and Windows keep returning None
Anybody can help me?
CodePudding user response:
So I came up with this:
Make sure your image is the same size as the one you're trying to find with your bot.
Put it in the same folder as the ".py" file as "btnLogin.jpg".
The working code should be like this:
import pyautogui
cond = True
while cond:
try:
x, y = pyautogui.locateCenterOnScreen('btnLogin.jpg')
#Click in Windows
pyautogui.click(x, y)
cond = False
except Exception as Ex:
print('Error: ', Ex)
This is basically the core of the code, doesn't cycle but should be fast enough if you just need to automate a login:
import pyautogui
try:
x, y = pyautogui.locateCenterOnScreen('btnLogin.jpg')
pyautogui.click(x, y)
except Exception as Ex:
print('ERROR: ', Ex)
Let me know if something isn't clear or doesn't work for you :)
CodePudding user response:
I set the window to open at a preset size, so I took all the prints I needed.
I checked to see if I was finding all the images, and I was finding them just right. But now I put it to run on Mac, to open the window with the same size, however, it doesn't find the images.
I don't know why, but even with the same window size, the image appears smaller on Mac :(
I open a window with the size (1440, 900) and took 2 pictures of the Login button. 1 picture in Mac and the other in Windows.
The size of the pictures:
- Mac = (207, 53)
- Windows = (253, 73)