Hi all so basically I’m new to python and using pyautogui
I have been able to do basic operations like moving the mouse to the specified coordinates
The problem
Pyautogui clicks on the button which is (Start learning Java now >>) on
w3 school website.
I want it to click and move to the next page but apparently, it clicks but there is no action.
in the ideal situation, it should click the button and move on to the next page but it doesn't do it although I see the click
Since I'm new to Python in general coming from a non-coding background.
I would love to have some opinion on this and a possible workaround
My operating system is Windows and my laptop is a Lenovo yoga s730
Below is the script/code Im using:
import pyautogui
import webbrowser
import time
webbrowser.open("https://www.w3schools.com/java/")
pyautogui.tripleClick(x = 700, y= 783)
Thanks
I guess you're trying to perform the click before the page has fully loaded, your code is quite correct, try this I hope it helps:
import pyautogui
import webbrowser
import time
webbrowser.open("https://www.w3schools.com/java/")
time.sleep(5) # wait for the page to load
pyautogui.click(700, 783)
if you would like to use some clever workaround the loading time use something like the concept refered to in this answer https://stackoverflow.com/a/56672841/13770671
Related
I am writing a python script that automates running a program and performing different tasks within the program. My main problem is figuring out how to click buttons and interact with the GUI of the program to be controlled.
I am currently using the pyautogui library and using pyautogui.click(X,Y) to advance through prompts and click on different menus and menu items. The problem with this approach is that I am relying on a separate script to inform me of the coordinates of interest in my environment by telling me the coordinates of where my cursor is hovering. This probably will not work on other machines and just seems like a one case solution.
My question is how can I automate using a program in windows (clicking around) without having to hard code the exact position of the items I need to click?
For example, If I need to click a "ok" box to accept some setting, how can I make Windows grab the program window, read through the options and click what I need without any prior knowledge of the position of the dialog box and where the "Ok" button is located?
Code:
import pyautogui as gui
gui.click(x,y)
The way you can do this using pyautogui is with their locating methods. You will need a picture (for example of the OK box) and then you can have pyautogui find it on the screen and give you its coordinates. Check out the official documentation on this.
I am trying to make a macro in Minecraft using pynput, but it seems that pynput does not press the correct buttons on the keyboard. I know this because when I ran (code snippet 1) while setting the controls of Hotbar slot 3 it showed as a weird character instead of a number. (see picture) Is there a way to make pynput press the correct buttons?
# Code snippet 1
import time
from pynput.keyboard import Controller
time.sleep(7) # this is just so I have time to switch to minecraft before it activates
board = Controller()
board.press('2')
time.sleep(.1)
board.release('2')
In place of '2' I have also tried KeyCode(49) and gotten the same result. It is properly recognised by the computer, as shown by keycode.info and notepad.
When I replaced '2' with 'e' Minecraft recognised it as the same as '2', and it even pressed the button, so I am extra confused now.
Extra notes:
For now I will just set the controls to what pynput gives minecraft to make my macro work, but I would prefer to be able to use the button(s) outside of the macro as well.
Pynput's click and scroll seem to work fine.
I am not using the mouse for my macro, but if you have knowledge about the mouse there is a stackOverflow question about it here: How to control the mouse in Minecraft using Python?
For anyone wondering, I am making this macro to empty water buckets quickly after I have used them to remove an area of water in the ocean
While I could be wrong about this, based on this stack overflow post I believe that pynput outputs the characters as win32 keycodes, which while they work for programs like notepad or keycode.info, don't seem to work with setting minecraft controls.
If you are ok with using a different library than pynput, I have found that pywinauto correctly inputs keys into minecraft.
from pywinauto import keyboard
import time
time.sleep(3) #A short wait to allow me to switch to minecraft
keyboard.send_keys("{2 down}" "{2 up}") #replace 2 with whatever key you want to press
Above is the code that I used while testing and I have found it works correctly when trying to set controls.
The easiest way i think is using pydirectinput.
pydirectinput is exactly like pyautogui. Its just that its name changed and that it can work with games using DirectX (minecraft, roblox)
To use it you can just do
import pydirectinput
import pyautogui
import time
time.sleep(2)#so u can switch to your game
pydirectinput.keyDown('v')
time.sleep(0.5)
pydirectinput.keyUp('v)
This is my first question so apologies in advanced for everything I'll do wrong in the next lines.
I am having issues with the doubleClick function from pyautogui on mac with OSX.
Lately I have written some python code using the following libraries
from time import sleep
from random import uniform as r
from pyautogui import click, doubleClick, press, moveTo, mouseDown, mouseUp
from webbrowser import open_new_tab
Even by trying the simplest combination
x1, y1 = (300, 375)
doubleClick(x=x1, y=y1)
there is no action taking place. I have tried two quick clicks with an interval, both also got no results from that.
I am experiencing this as a major issues and have checked now on several mac machines (5+) and the issue is always there and I cannot find any reference in other questions or on the internet.
Is anyone else experiencing the same?
Thank you already for your help.
Had the same problem. Found a fix here. (Just make sure you first activated the window you need to double click in, with a single click. I forgot and now it works perfectly.)
I am writing a script to automate repeatedly registering new users for a website (not boosting metrics, not what you think!). I can boil down the process of registering to a series of mouse clicks and typing. I know there are some macro recorders that will let me record how I use the GUI and repeat it, but I need to type something a little different every time - however the mouse clicks are always the same.
Imagining script to look kindof like:
username = "something"
for i in range(0,100):
playback recorded series of mouse clicks A
type username + str(i)
type some other stuff
playback recorded series of mouse clicks B
But I can't find a good tool to record a series of mouse clicks so that I can play them back from either an applescript or a python script.
I found a useful script (http://www.bluem.net/en/mac/cliclick/) I can call that takes in 2 coordinates and clicks. So even if I could find a tool to record the coordinates of mouseclicks that would work!
You can also run JavaScripts in browsers with something like tell application "Safari" to tell document 1 to do JavaScript or tell application "Google Chrome" to tell tab 1 of window 1 to execute javascript.
tell application "Safari" to tell document 1
do JavaScript "document.querySelectorAll('.mainnavs ul li:nth-child(2) a')[0].click()"
delay 1
repeat until do JavaScript "document.readyState" is "complete"
delay 0.1
end repeat
do JavaScript "document.getElementById('tagfilter').value = 'aa'"
end tell
System Events has commands for simulating clicks and keystrokes:
delay 1
tell application "System Events" to tell process "Safari"
click at {76, 117}
repeat with i from 1 to 3
keystroke "aa" & i & return
end repeat
keystroke "a" using command down
end tell
You can see the positions of UI elements from Accessibility Inspector.app. It is part of Xcode, but it can also be downloaded from developer.apple.com/downloads.
If you want to get the screen coordinates of where you want to perform mouse clicks, you can use a free tool I made called MouseTools found here. I made it for just this purpose. You'll want to do something like I show in the example #1 applescript near the bottom of the page. Just move your mouse to a position on the screen and run the applescript to get the coordinates. Note: you'll want to move your mouse to a location and press command-r (while the applescript is frontmost) to run the script in AppleScript Editor.
In addition, I found that clicking at screen coordinates, as Lauri Ranta showed in her applescript using System Events, often does not reliably work. System Events is good at clicking in the Finder however it often has trouble clicking in the window of an application like Safari. If you find that problem too then you can also use the MouseTools to perform the mouse clicks. Again, this is why I created the program and there are examples of how to do this on the web page.
I hope it helps. Good luck.
How about Carsten Blum's Clicclick?
His link
It is a shell app which will emulate mouse clicks at arbitrary screen coordinates. This will work with applescript.
Hopefully you are doing test automation!
Have you looked into using something like Selenium WebDriver instead?
I want to open a page and grab the screen and obtain its mean color with PIL. I want to open about 100 pages so I thought a screen capture script with Python would be useful. I found several of them and I decided to use this simple one:
"""
A simple screen grabbing utility
#author Fabio Varesano -
#date 2009-03-17
"""
from PIL import ImageGrab
import time
time.sleep(5)
ImageGrab.grab().save("screen_capture.jpg", "JPEG")
But I need to run this from Command Prompt which then shows on the screen. How do I make this into a script so that I can call it programmatically? Or, what do you think is the best way to achieve this? Thanks!
You can use Selenium and any browser:
Take a screenshot with Selenium WebDriver
This is cross-platform, works on every browser.
Selenium is designed for functional web browser testing, but is suitable also for use cases like yours. More info about Python + Selenium:
http://pypi.python.org/pypi/selenium
The benefit of Selenium is that you have fine-tuned control of the browser - you can click buttons, execute Javascript, whatever you wish and the browser is no longer a black box.
This solution seems to do exactly what you want by bringing the browser to the foreground before performing the screen grab. Just replace 'firefox' with 'chrome'