## Warning: package 'reticulate' was built under R version 4.1.3
Workforce Management encompasses a large scope of roles. Data gathering and analysis, forecasting, capacity planning, scheduling and real-time analysis are the most common ones among them. While whether or not you like the roles depends on personal taste and experience level, some of the daily tasks in any one of the roles can be repetitive, laborious and therefore boring. The good news, however, is that repetitive tasks can be automated. In this blog, I will introduce some some automation tools and techniques that are very popular in the task automation world. This is a very brief introduction to a very wide world. I will provide supporting links in every section so that you can learn more at least until I go through each section separately in future blogs.
Imagine you having to download the same report for all your agents separately. It can get worse if you have to do that day by day. This task is basically one big loop of changing the agent name and date, short waiting, and then pressing the download button. It literally involves some mouse clicks and 2 keyboard text insertions. Here are some Python and R solutions which can do that and many more.
Click here for a quick introduction to Python.
pyautogui is a well known python module for mouse and keyboard automation. This module is not preloaded with Python. So to install it run the following command on the terminal once you have Python installed on your device.
pip3 install pyautogu
Import the module to the environment
import pyautogui
Some use cases are
a. Get Screen resolution
Screen resolution is defined by coordinates X, Y. Resolution size of 2000X1000 means that the screen is 2000 pixels wide and 1000 pixels tall. The pixel on top left corner is pixel (0,0) and the last pixel is at the bottom right corner. Every other position is between those two.
print(pyautogui.size())
b. Get current position of the mouse
print(pyautogui.position())
c. Click
pyautogui.click(100, 100) # left click
pyautogui.rightClick(100, 100) # right click
pyautogui.middleClick(100, 100) # middle click
pyautogui.click(100, 100, clicks=2) # double click
d. Scroll
To scrolls the screen up to a given number of pixels
pyautogui.scroll(200)
e. Move the mouse
To move the mouse to a new location
pyautogui.moveTo(100, 100, duration = 1)
pyautogui.moveRel(0, 50, duration = 1) # relative to its previous position
pyautogui.dragRel(0, 100, duration = 1)
f. Keyboard type string
pyautogui.write('Hello WFM')
g. Press Hotkeys
pyautogui.press('enter') # press the Enter key
pyautogui.press('f1') # press the F1 key
pyautogui.press('left') # press the left arrow key
pyautogui.hotkey('ctrl', 'c') # Ctrl + C = copy
pyautogui.keyDown('shift') # hold down the shift key
pyautogui.keyUp('shift') # release the shift key
f. Screenshot
pyautogui.screenshot('screenshot.png',region=(0, 0, 300, 400)) # takes screenshot of a 4X4 region and saves as .png file.
‘time.sleep(nseconds)’ from ‘time’ library can be used for waiting time between actions.
import time
time.sleep(5) # sleep for 5 seconds
There is a lot more you can do with this library says the official documentation.
Click here for a quick R tutorial.
If R is your favorite language like it is for me, KeyboardSimulator package is what you need for this.
You will need to install and load the package.
install.packages("KeyboardSimulator")
library(KeyboardSimulator)
a. Mouse click
mouse.click(button = "left", hold = FALSE) # "right", and "middle" are also allowed. hold = TRUE holds the key down
mouse.click(button = "right", hold = TRUE)
mouse.release(button = "right")
b. Get mouse cursor location
mouse.get_cursor() # returns X, Y coordinates of the current location
c. Mouse move
mouse.move(x=960,y=540,duration=3) # move to x, y in 3 seconds
d. Keyboard press
keybd.press('a') # press one key
keybd.press('Alt+F4') # press multiple keys
# press multiple keys using hold
keybd.press('Alt', hold = TRUE)
keybd.press('F4')
keybd.release('Alt')
e. Keyboard type string
keybd.type_string("Hello WFM!")
Use ‘Sys.sleep(time)’ to suspend execution for a time interval (time in seconds).
Real Time Analysts need to constantly monitor queues a web page or many webpages and send queue updates through a communication channel to agents and their supervisors. Forecasting analysts sometimes go through multiple web pages to gather historical data. Creating daily, weekly, monthly reports can take hours of browsing. Here are the web scraping solutions you can easily apply to your problem.
Web scraping is an automated process of gathering information from the websites. Websites are built with HTML, an abbreviation for HyperText Markup Language, sometimes assisted by technologies such as CSS, cascading style sheets. HTML is a “tagging” language. The tags tell the web browsers how to display things. It is important to understand the basics of HTML to be able to inspect your website which is a key part of web scraping. Check this tutorial for a quick introduction to HTML and this for web element inspection on your favorite browser.
Some websites don’t like it when automatic scrapers gather their data. Robots exclusion standard is how websites communicate with web crawlers and other web robots. This is used mainly to avoid overloading your site with requests. Your request to certain URLs is either allowed or disallowed based on what is in robots.txt file of the website. Here is Facebook’s robots.txt file. It’s necessary to abide by the
With that you should be ready for the what is coming next.
Click here for a quick introduction to Python.
A. Beautiful Soup
Beautiful soup is a python library which is great for scraping static websites. A static website is one with stable content, where every user sees the exact same thing on each individual page. On the other hand, allows its content to change with the user and therefore require login.Wikipedia is an example of a static webpage.
To install Beautiful Soup, run this command on the terminal.
pip install beautifulsoup4
Use ‘requests’ library to request the html from the website.
import requests
url = 'https://someurl'
html = requests.get(url)
Parse the html using html parser of beautifulsoup4 library.
from bs4 import BeautifulSoup # import the library
htmlParsed = BeautifulSoup(html.text, 'html.parser')
There is so much you can do with the parsed html. Please refer to the documentation for more.
B. Selenium
Unlike Beautiful Soup, Selenium is not a Python library. It’s rather a framework for a range of tools and libraries that enable and support the automation of web browsers. Selenium can scrap both static and dynamic webpages. It can be used with various programming languages including Python. At the core of Selenium is WebDriver, an interface to write instruction sets that can be run interchangeably in many browsers. We will need to install both Selenium and the WebDriver if we have not already done that.
Download and install webdriver for your favorite web browser ( Chrome, Mozilla).
To install Selenium
pip install selenium
This is how you would get a webpage using selenium.
from selenium import webdriver
driver = webdriver.Chrome()
driver.get("http://selenium.dev")
driver.quit()
Please refer to the documentation here a whole lot of things you can do with Selenium.
Sending repetitive emails or chat messages can be time consuming and boring. While automating this can be done with one or combination of the above techniques, many commercial messaging platforms such as Slack, Microsoft Teams, Gmail provide APIs that can make it easier for you.
I hope this blog inspires you to automate some of your routine tasks. I am crazy about data science and applying data science skills to workforce management. Reach me at LinkedIn if you wish to connect :)