Cannot click on game window in Python - python

I was trying to make a bot for a game, which you can find here. You have to play games to get a tiny amount of bitcoin, like satoshis. Anyway, I as going to make a bot, but when I tried to click on the game window, it didn't do anything. Here is my code:
clicker.py
import pyautogui
while True:
x, y = pyautogui.position()
pyautogui.click(x, y, button='left')
I don't know what's going on, is it my code, or is it the website html? If it is the html, what should i change?

What may be happening is that when you run the program, your mouse clicks so fast that the site does not recognize the clicks. But anyways, how the program is supposed to stop? Your mouse will be clicking everywhere and you may not be able to close the Python window. Do something like this:
import pyautogui
from time import sleep
from threading import Thread
def click_function():
while True:
x, y = pyautogui.position()
pyautogui.click(x, y, button='left')
sleep(0.5) # an acceptable waiting time between one click and another
t = Thread(target=click_function)
t.daemon = True
t.start()
sleep(60)
# Here you put the amount of time you want the program
# to run, in seconds. This will ensure that the program
# stops sometime and you don't get stuck.
You can try adding sleep(n) at the beginning of the code. That is, you run the program, enter the site normally and point your mouse at the specific part where you want to click. Change the value of n to a considerable amount of time in seconds you can perform these tasks. If this doesn't work, you can try the Pynput module, which works similarly to PyAutoGUI.

Related

Simulate Mouse in Python doesn't work (Windows)

I am quite the beginner and have the folloing problem: I am trying to automate a mouse movement and some key presses and the code works until I select the program window. (So I can run it and use it for every program I tried except the one I want to use it for). The code still runs but the mouse movement it should do does simply not happen and I don't know why.
I have tried it using win32api, win32con and pyautogui both failed. So is there any possibility to make the mouse movements more like real mouse movements? or does anyone have an idea why it might not work?
def click(x,y):
win32api.SetCursorPos((x,y))
win32api.mouse_event(win32con.MOUSEEVENTF_LEFTDOWN,x,y,0,0)
win32api.mouse_event(win32con.MOUSEEVENTF_LEFTUP,x,y,0,0)
click(x, y)
and
pyautogui.click(x, y)
From the comments:
As the target application is running with administrator privileges, a nonprivileged program can't simulate events on it.
To work around that, run your script as administrator.

In pyautogui, it seems to be running in only one window

I am making a bot at the moment using pyautogui. When I run it in pycharm, it clicks on wherever my mouse is after 10 seconds, but if I use it on google, it runs but does not click. here is the code:
import pyautogui
import time
time.sleep(10)
while True:
pyautogui.click()
does anyone know how to fix this (btw i'm using chromebook)

Is pynput capped at how fast it can run?

For example, with pyautogui, you can allow it to go as fast as you want with pyautogui.PAUSE = 0
Is there something similar for pynput? (Allow it to run as fast as my computer lets it?)
well I think not but u could theoretically try messing with while cycle so it would just press and release the mouse button (if it wont register the clicks u could try to import time and experiment which is the minimal delay of seconds to register those clicks)
it would look like something this:
while True:
mouse.press(Button.right)
time.sleep(0.001) #experiment with this number and see which works best
mouse.release(Button.right)

Python Mouse click simulate halts program until you press CTRL+C

I'm just trying to simulate a mouse click every second but once the first click starts, the program just completely halts and I have no idea why
The program will continue if I press CTRL+C but then it halts again after it clicks:
import pyautogui
import time
while True:
time.sleep(1)
print("Clicking")
pyautogui.click()
And it doesn't matter which mouse library I use to simulate the click, it always halts. It should be clicking every second without interruption. What's wrong?
The only reason i think it does so is because it clicks at some place where it is not supposed to(by default the position is your current mouse pointer), so the pyautogui.click() takes 2 parameters x,y try specifying the the exact coordinate and it should work. To find the coordinates of a position place your mouse pointer there and run pyautogui.position().

Why is KeyboardInterrupt not working for Python pyautogui script? Alternative way to exit program/loop?

I am trying to complete a simple GUI automation program that merely opens a web page and then clicks on a specific spot on the page every 0.2 seconds until I tell it to stop. I want my code to run and have its loop run infinitely until a keybind I specify breaks the loop (or entire program). I started out with the classic KeyboardInterrupt, which supposedly enables CTRL+C to exit a program. Here is my code:
import webbrowser, pyautogui, time
webbrowser.open('https://example.com/')
print('Press Ctrl-C to quit.')
time.sleep(5)
#pyautogui.moveTo(1061, 881)
try:
while True:
time.sleep(0.2)
pyautogui.click(1061,881)
except KeyboardInterrupt:
print('\nDone.')
Unfortunately, KeyboardInterrupt and using CTRL-C to exit do not seem to work for this script (likely due to the while loop?). This causes the loop to continue to run infinitely without a way to be stopped. So my questions are: why isn't the Keyboard Interrupt working? I've seen similar examples in other scripts. Additionally, if the KeyboardInterrupt doesn't work, is there a way I can code a simple keybind to exit the program/loop?
Use the following code
pyautogui.FAILSAFE = True
Then to stop, move the mouse to the upper-left corner of the screen
I suspect it may have something do to with you having a different active window than the script; when you use webbrowser, open a webpage, and click on it, it moves your active window to the webpage rather than the Python console. So ctrl+c will only produce a KeyboardInterrupt when the console is your active window. Your script may be in fact correct; but your active window is not on Python, so you would have to test it by clicking back into the Python console while the program runs.
To answer your comment: No, I do not know of any other "quick" way to do such a thing.
I'm late, but I can provide a solution which allows you to press CTRL + C to stop the program. You need to install the keyboard module and use keyboard.is_pressed() to catch when you press the keys you want as flag:
import keyboard
# You program...
while True:
time.sleep(0.2)
pyautogui.click(1061,881)
if keyboard.is_pressed("ctrl+c"):
break
You need to be careful though because the program will check if you have pressed the keys only when it executes the if statement. If your program runs for 10 seconds and you place the if at the end, you will only be able to exit every 10 seconds and for a very brief moment.
I also suggest to keep the keys pressed while you wait for the program to catch them to avoid missing the moment.
If you instead need to instantly terminate the program without having to always check if CTRL+C are pressed, you can place it in a process and kill it whenever you want. It's a bit overkill and it's not the recommended way, but if you really need it, here it is.
import pyautogui
import keyboard
import time
from multiprocessing import Process
def execute_program():
"""Long program which you want to interrupt instantly"""
while True:
pyautogui.click()
time.sleep(10)
if __name__ == '__main__':
# The failsafe allows you to move the cursor on the upper left corner of the screen to terminate the program.
# It is STRONGLY RECOMMENDED to keep it True
pyautogui.FAILSAFE = True
# Spawn and start the process
execute_program_process = Process(target=execute_program)
execute_program_process.start()
while True:
if keyboard.is_pressed('ctrl+c'):
execute_program_process.terminate()
break
print('\nDone.')

Categories