You may come across tasks that require you browsing a website and clicking on certain links, downloading files on a daily basis. If so, this code has proven useful to me, as it allows me to delegate the downloading to the python script.
You will need to install several modules: webbrowser, pynput, time, keyboard
Step 1: Script to Open Webbrowser
First step, you need to call the desired website address in your webbrowser. I actually used this only at the end, but recommend it at the beginning. This will mean the coordinates you get from step 2 will be the same every time you run the script. (At the beginning I was manually opening the browser tab and scrolling, instead of using the key down to get a uniform position each time)
# importing webbrowser python module
import webbrowser
#Assigning URL to be opened
strURL = "https://your.source.of.data.website"
#Open url in default browser
webbrowser.open(strURL, new=2)
Step 2: Listener from pynput
Second step, you need to get the coordinates on the website and links you are navigating to. The script below allows you to use pynput (pip install pynput) and then use its keyboard and mouse listening function. The position, i.e. coordinates will be save to a txt file „log.txt“, from which you will be able to read the relevant ones. This script keeps running until you close the console. Also it does not seem to log anything except on_click. (If you know why, please drop me an email or comment below, much appreciated!)
from pynput.keyboard import Listener as KeyboardListener
from pynput.mouse import Listener as MouseListener
from pynput.keyboard import Key
import logging
logging.basicConfig(filename=("log.txt"), level=logging.DEBUG, format='%(asctime)s: %(message)s')
def end_rec(key):
logging.info(str(key))
def on_press(key):
logging.info(str(key))
def on_move(x, y):
logging.info("Mouse moved to ({0}, {1})".format(x, y))
def on_click(x, y, button, pressed):
if pressed:
logging.info('Mouse clicked at ({0}, {1}) with {2}'.format(x, y, button))
def on_scroll(x, y, dx, dy):
logging.info('Mouse scrolled at ({0}, {1})({2}, {3})'.format(x, y, dx, dy))
with MouseListener(on_click=on_click, on_scroll=on_scroll) as listener:
with KeyboardListener(on_press=on_press) as listener:
listener.join()
Step 3: Mouse Controller Function from pynput, Click Button via Coordinates Position, Keyboard Key Down Controller
In the third step you can use a combination of the mouse controller function in pynput and click button from controller. We get the coordinates for the mouse controller from the previous script, that listened for our clicks. At the same time you include the sleep function from time. This allows your code to function properly. Without a sleep function the code tries to execute immediately, which did not work when I was testing.
# Import pynput
from pynput.mouse import Button, Controller, Listener
import time
# how long should we sleep, in seconds
slp = 1
print('trying first click')
try:
mouse1 = Controller()
mouse1.position = (713, 111) # this is the key, to enter correct coordinates from log
mouse1.click(Button.left,1)
pass
except:
print("An exception occurred")
time.sleep(slp) # we use sleep to give the browser time to load and execute
print('trying second click')
try:
mouse2 = Controller()
mouse2.position = (1134, 596)
mouse2.click(Button.left,1)
pass
except:
print("An exception occurred")
time.sleep(slp)
# Using Keyboard module in Python
import keyboard
# Using the arrow down keyboard command to move to the
# desired part of the webpage (or any other key command)
try:
keyboard.press_and_release('down, down, down')
pass
except:
print("An exception occurred")
time.sleep(slp)
With these three scripts combined I was able to reduce the time it takes to download over 20 documents to just over a minute, using over 60 clicks with the pynput controller.
So every day a few minutes saved from a menial, repetitive task.
Questions or feedback?
Feel free to write me at contact@zuberbuehler-associates.ch or add a comment.