How to click a word on screen with pyutogui - python

The error I am getting is:
OSError: Failed to read Ok because file is missing, has improper permissions or is an unsupported or invalid format.
Can someone help me?
I’m using the command:
pyautogui.click(‘Ok’)
I was expecting this to click Ok on the screen when it pops up.
My code is:
from pyvirtualdisplay import Display
from selenium import webdriver
from pynput.keyboard import Key, Controller
from pynput.mouse import Button, Controller as MController
import pyautogui
import time
def Mid():
keyboard = Controller
mouse = MController()
pyautogui.press('win')
pyautogui.write('Change user account control settings')
time.sleep(1)
pyautogui.press('enter')
time.sleep(2)
pyautogui.leftClick('OK')

pyautogui.leftClick('OK') will do nothing since pyautogui.click() will take x and y coordinates for example:
pyautogui.click(100,100) #will click at the coordinates x 100 and y 100
We can use locateOnScreen to get x,y coordinates of a image and then click it with pyautogui.click
In your case:
time.sleep(2)
# Find yourimage.png in your screen
button = pyautogui.locateOnScreen("yourimage.png")
# Click at x,y of where the button is found on the screen
pyautogui.click(button)

Related

Roblox won't detect mouse movement from autopygui

I am trying to create a script that automatically rejoins a roblox game on disconnect. I have beeen using ctypes to obtain a pixel on the screen, and if the pixel matches a color, it should automatically press the rejoin button. the problem is that it wont press the button. After some troubleshooting, I have figured out that the mouse movement wont register with the game, as if I move my mouse manually, it clicks the button.
In short, the game won't detect mouse movement from autopygui. If I move my mouse manually, it registers.
Video example:
https://youtu.be/VvAfHHXul8Q
Code:
import pyautogui as py
import keyboard
import tkinter
import requests
from ctypes import windll
from time import sleep
key = "m"
toggled = False
rjcolor = 16777215
root = tkinter.Tk()
root.withdraw()
width, height = root.winfo_screenwidth(), root.winfo_screenheight()
dc= windll.user32.GetDC(0)
def getpixel(x,y):
return windll.gdi32.GetPixel(dc,x,y)
while True:
if keyboard.is_pressed(key):
toggled = not toggled
print("toggled to " + str(toggled))
sleep(0.5)
if toggled == True:
py.moveTo(width / 2, 800)
py.click(button='left')
if getpixel(1050, 600) == rjcolor:
print("disconnected, waiting until internet back online!")
while True:
try:
requests.head("http://www.google.com/", timeout=3)
print('The internet connection is active, rejoining.')
py.moveTo(1050, 590)
py.mouseDown(button='left')
sleep(0.1)
py.mouseUp(button='left')
break
except requests.ConnectionError:
print("The internet connection is down")
sleep(3)
sleep(0.1)
Pyautogui has issues with clicking on roblox, but i've found a workaround:
Replace py.click(button="left") with autoit.mouse_click("left")
import autoit
autoit.mouse_click("left")

PYTHON | Apps Unable to Take Automated Mouse Input

Recently after upgrading to Windows 11, I've found that some apps can't receive automated mouse and keyboard actions. The script simply goes to a location and clicks but after going to the location, the program doesn't respond to the clicks. Further more, the mouse cursor doesn't change to indicated that the element is clickable. I've tried several methods already, PyDirectInput, Pynput and Pyautogui.
I'm running the script to the application Roblox and I'm using Pycharm as an IDE.
Script with pydirectinput:
import pydirectinput
import pyautogui
import time
x, y = pyautogui.size()
def click_ratio(xr, yr):
global x, y
pydirectinput.click(int(x/xr), int(y/yr))
pyautogui.alert()
time.sleep(1)
click_ratio(1.057414291615035, 1.6881594372801876)
Script with pynput:
from pynput.mouse import Button, Controller
import pyautogui
import time
x, y = pyautogui.size()
mouse = Controller()
def click_ratio(xr, yr):
global x, y
mouse.position((int(x/xr), int(y/yr)))
mouse.click(Button.left, 2)
pyautogui.alert()
time.sleep(1)
click_ratio(1.057414291615035, 1.6881594372801876)
Script using pyautogui:
import pyautogui
import time
x, y = pyautogui.size()
def click_ratio(xr, yr):
global x, y
pyautogui.click((int(x/xr), int(y/yr)))
pyautogui.alert()
time.sleep(1)
click_ratio(1.057414291615035, 1.6881594372801876)
The clicking works fine on Chrome. If anyone could help me, it would be greatly appreciated.

Why doesn't my Python script work in game?

I've been trying to make a simple script, it presses the key when the image of the key is visible in the left bottom corner of the screen.
The problem is when I'm in the game (Multi Theft Auto) the script stops working only in the game window. I tried running script/game as administrator, changing resolution, making the game windowed/fullscreen but nothing changes. Here's my code:
from numpy import random
import pyautogui
import pydirectinput
import time
import keyboard
import random
import win32api, win32con
sleeptime1 = random.uniform(1.05, 2.03)
sleeptime2 = random.uniform(1.04, 2.01)
sleeptime3 = random.uniform(1.02, 2.05)
while True:
if pyautogui.locateCenterOnScreen('key_a.png', region=(0,580,500,500), grayscale=True, confidence=0.7) != None:
pydirectinput.press('e')
time.sleep(sleeptime1)
if pyautogui.locateCenterOnScreen('key_b.png', region=(0,580,500,500), grayscale=True, confidence=0.7) != None:
pydirectinput.press('q')
time.sleep(sleeptime2)
else:
pydirectinput.click()
time.sleep(sleeptime3)
just run your game as administrator like your python, that should help.

Python Not Clicking in game or Webbrowser

I am making my own autoclicker. But its can not clicking. I try all alternatives which ones in stackoverflow. Is there a any helper please.
My Code:
import pyautogui
import python_imagesearch.imagesearch
import time
while True:
icon = python_imagesearch.imagesearch.imagesearch("clickme.png", 0.8)
print(icon)
icon_x = icon[0]
icon_y = icon[1]
icon_x = icon_x+5
icon_y = icon_y+5
# time.sleep(1.5)
pyautogui.leftClick(icon_x, icon_y)
time.sleep(0.1)
pyautogui.leftClick(icon_x, icon_y)
time.sleep(0.1)
I am trying the click here.
https://www.tetralark.com/ClickerJs/
It's solved. It is about other games anticheat blocking.
If your code is moving to your image already, you can click in the image by using:
pyautogui.click()
Or you can pass coordinates as Mouse Control Functions says:
pyautogui.click(x=100, y=200) # move to 100, 200, then click the left mouse button.

Simulate real click python

As I can simulate a click as if it were made from the mouse, a game has detection of click events or simulated keys, I would like to know if it is possible to simulate a real click.
Translated.
My code:
import time
import keyboard
import pyautogui
from playsound import playsound
pyautogui.PAUSE = 0.2
sW, sH = pyautogui.size()
down = False
while True:
if keyboard.is_pressed('f4'):
if not down:
down = True
playsound('on.mp3')
pyautogui.moveTo(sW*0.67, sH*0.5)
pyautogui.click()
pyautogui.moveTo(sW*0.67, sH*0.6)
pyautogui.click()
pyautogui.moveTo(sW*0.67, sH*0.64)
else:
down = False
This code simulates a real click of the mouse.
import win32api, win32con
from time import sleep
def click(x,y, duration=30, delay=0.3):
win32api.SetCursorPos((x,y))
win32api.mouse_event(win32con.MOUSEEVENTF_LEFTDOWN,x,y,0,0)
sleep(duration / 1000)
win32api.mouse_event(win32con.MOUSEEVENTF_LEFTUP,x,y,0,0)
sleep(delay)
Why?
Because the lib win32api is directly connected with the windows commands.
When u click with the mouse, you actualy triggers a function inside the windows.
win32api does de same.
Also...win32api is the fastest comunication with the windows for this stuff, like image rendering

Categories