So I'm trying to automate my Manga reading and I'm coming into the trouble of finding out how to set a duration for each event. For example if I want it to scroll for 200 seconds then click the next page at X & Y coordinates, how would I go about doing this?
Note: I understand how to make pyautogui click. I'm more concerned on figuring out how to make it time delay.
import pyautogui
speed = input('how fast should it scroll')
sleepTime = input('how long before next scroll')
pyautogui.time.sleep(3)
while 0 < 10:
pyautogui.moveTo(918, 492, duration=26, tween=pyautogui.easeInOutQuad)
pyautogui.scroll(int(speed))
pyautogui.time.sleep(int(sleepTime))
pyautogui.FAILSAFE = True
Taken from the PyAutoGUI docs
Instead of:
pyautogui.time.sleep(3)
Do this:
pyautogui.PAUSE = 3
Related
I have been learning python recently and decided to learn more about pyautogui, what you see down below is a macro of mine. My question is simple; is there a way to let this macro run in a specific window. For example: I want this macro to run in google chrome while I am in discord chatting with my friends (text channel so I'm not in the google chrome window). (Ignore my sloppy method of writing code)
import pyautogui
import random
import time
import mouse
#############################
tijd = 0
actief = 0
float (actief)
#############################
while not mouse.is_pressed('right'):
time.sleep(0.01)
bank_x1, bank_y1 = pyautogui.position()
time.sleep (0.5)
while not mouse.is_pressed('right'):
time.sleep(0.01)
bank_x2, bank_y2 = pyautogui.position()
print ("{} {} {} {}".format(bank_x1,bank_x2,bank_y1,bank_y2))
#############################
lijst = [[bank_x1,bank_x2,bank_y1,bank_y2,200,243],[1203,1236,721,749,23,49],[390,422,112,140,22,46]]
while not mouse.is_pressed('middle') or actief > tijd:
for i in range(0, 4):
x = random.randint(lijst[i][0], lijst[i][1])
y = random.randint(lijst[i][2], lijst[i][3])
pyautogui.moveTo(x, y)
wacht = random.randint(lijst[i][4], lijst[i][5]) / 100
time.sleep(wacht)
str (actief_str)
pyautogui.click()
pyautogui.press('esc')
No. Not with pyautogui.
Pyautogui simulates actual keyboard and mouse input to the system, not to a specific window. Thus, it will always act exactly the same as if you were pressing the keys and clicking the mouse yourself. So, no. You cannot use it to send keystrokes to background applications. The keystrokes will always effect the window that is focused, just as they do when you physically input them with a keyboard or mouse.
This question shows how you could achieve it with winapi, but it is much more complicated and less user-friendly than pyautogui.
my question is actually quite easy but I get stuck with this issue.
I want to set timer to 2 second and then check if the mouse is still in the same poit as previous.
example: I detect the point of the mouse as (250, 500) and then put timer to 2 secs and check again where is the pointer now.
would appreciate your help :)
you can use time.sleep in cycle for
import time
for i in range(0,2):# range: 0,1 = 2 iteration = 2 sec
time.sleep(1)
So, this piece of code:
import pyautogui
pyautogui.PAUSE = 0
pyautogui.MINIMUM_SLEEP = 0
pyautogui.MINIMUM_DURATION = 0
pyautogui.moveTo(100,100,duration=1)
Makes the mouse takes 20+ second to reach its destination. The cause is the MINIMUM_SLEEP value, no matter the duration added it will take the same time (ages). Is there a way around this? - Is there any way to make the mouse movement smooth and still set the duration?
I am very surprised I haven't seen any comments on this. Thanks.
I need to run a code for hours, and the computer I am working with has a (forced and unchangeable) screensaver policy. (It locks after 10 minutes). What can I do to prevent it? Is there any line of code to prevent that?
This worked for me. I'll just leave it here so people can use it.
import ctypes
ctypes.windll.kernel32.SetThreadExecutionState(0x80000002) #this will prevent the screen saver or sleep.
## your code and operations
ctypes.windll.kernel32.SetThreadExecutionState(0x80000000) #set the setting back to normal
You can prevent screen saver by moving mouse cursor on a fixed period of time, below function can handle cursor moving.
import win32api
import random
def wakeup():
win32api.SetCursorPos((random.choice(range(100)),random.choice(range(100))))
This is a coded solution that you can place in your program (also works for Mac users):
pip3 install pyautogui
https://pypi.org/project/PyAutoGUI/ (Reference)
import pyautogui
import time
def mouse_move():
while True:
pyautogui.moveTo(100, 100, duration = 1) # move the mouse
time.sleep(60) # Every 1 min
pyautogui.moveTo(50, 100, duration = 1) # move the mouse
mouse_move()
Or, without the while loop, run it when required if your program is already within a while loop:
def mouse_move():
pyautogui.moveTo(50, 100, duration = 1) # move the mouse
mouse_move()
I have a question about Pyautogui. I want to click on a button/link/image, but in my country the Internet speed is very slow. So I'm trying to delay the program. But sometimes it doesn't work. The program clicks on the link/image/button before it appears on the website. That's why I cannot continue or the whole program fails.
Is there any way the program can locate if the button/link/image is displaying or not? If it is not displaying, it should wait for 30 more seconds or 1 minute. That's how I want my program to work every time. I really want to solve this issue. I hope you will reply.
This may be tricky if you are looking at a bunch of different webpages all with different content. But if you know a specific image or link text that must be there, you should be okay.
max_wait below is so the program doesn't loop endlessly. Make it as big as you want.
For a specific image you know has to show up in the page, you can try
import time
import pyautogui
found_image = False
max_wait = 120
x = time.time()
while (found_image == False) and time.time() - x < max_wait:
if (pyautogui.locateOnScreen('my_png.png') is not None):
found_image = True
if found_image: do_main_test()
For text, you can use pyautogui to select all...
import time
import pyautogui
import pyperclip
x = time.time()
found_text = False
max_wait = 120
while (found_text == False) and time.time() - x < max_wait:
pyautogui.click(150, 150) # or wherever is in the window
pyautogui.hotkey('ctrl', 'a')
pyautogui.hotkey('ctrl', 'c')
pyautogui.click(x=150, y=150)
lines = pyperclip.paste()
found_text = needed_text in lines
if not found_text: sleep(5) # this is so pyautogui isn't continually highlighting etc.
if found_text: do_main_test()
I don't know about tracking buttons, but I hope the text and image examples are useful. And of course you can specify running do_main_test() only if found_text and found_image are true.