My dear Python enthusiasts over there! Today I brought a little hack for making your life a little less boring. You can use this idea to build very complex algorithms.
Getting the libraries:
I assume you have Python installed on your computer. If not and you still want to follow this instruction, please install python any version will do.
Today we will show you how can you automate some tasks automated just using a few functions from pyautogui library.
Most probably your python comes with this library, if not go ahead and pip install it.
command: pip install pyautogui.
Or using anaconda.
command: conda install -c conda-forge pyautogui
Clicking with Python:
Let me tell you first what will we do today. We will use the auto click function from the GUI library and make our program click where we want.
Let’s say we want to click on the close button to quit the window. So the question is where will our program click. to find out the coordinate position of the close button let’s open python in the command prompt or anaconda prompt or any of your terminal.
Then import the required library.
import pyautogui as gui
then type the command
gui position() (don’t press enter)
hover your mouse on the close button ( just hover, don’t click anywhere).
then press Enter you will see the script has detected the position of the close button (or wherever you were hovering the mouse pointer).
Now we found the coordinates of the button we want to click let’s simulate the click using click function. Click method requires two arguments. X and Y coordinate of the position.
gui click(1895, 22)
Now let us make our program more useful.
Let’s think of something we need to automate. I have joined Fiverr recently and for a beginner, there isn’t a lot of posts to bid!
So refreshing the Buyer Requests page might be a good idea for me.
so let us find the position of the active button where the active jobs will be reloaded.
and now using the program click!
Looping and using Time:
So we want our program to click this button and check if there is any job every 30 seconds.
To do that we can run the program in a while True loop and use the time library.
while True: gui.click(1962, 289) sleep(30)
so our program will loop over time and click the active button every after 30 seconds.
Now let us check if there is a new job!
we can detect the color of any given position to see if there is a job. Like if there is a job there will be this green send-offer button.
for that, we will first find the position coordinate and then use the pixel method which takes the position arguments. which will check the color of the position. if there is no job this place will be light grey. (#RGB 247,247,247). But if there is a job there will be a send-offer button which is green.
gui.pixel(3058,453) #then let us add condition
if c != (247,247,247):
This code will check if the button is not grey then click the button for us and print Attention on the terminal.
Let us say we want the program beep as well to notify us.
let’s import another library which will play output sound. And use the Beep class to play the sound when there is the green button. Beep class will take two arguments, frequency of the beep and duration.
we added extra 60 second to let us write a proposal if there is a job.
Now to not be disappointed we need to expect uncertainty!
i will keep track how many times did our program loop.
Download or fork the code on GitHub: Github Python
I hope you are familiar now with python automation. Use the idea to build complex algorithms.
You can use pyinstaller to make a exe file from the python file. And create sharable bots.
Thank you for staying with datalators. Subscribe our newsletter to make sure you don’t miss any post.