An Email Bot for the Masses
How a little Selenium knowledge can go a long way
What’s a bot?
A bot is a program that interacts with web elements (elements from a website, web application, etc.). It makes it seem as if an actual person is using some web application, when in reality, it’s really a program. Bots can be used for things like: testing website functionality, producing information for a user within an application, and generating conversational messages (chatbot).
Email bot explanation
The bot detailed in this article focuses primarily on sending emails (an email bot). Outlook will be the email of choice for the derived implementation. I will use Python along with the Selenium framework to create the desired email bot. The image above shows the URL from the Outlook email Sign in page. This URL is used as the starting point for the bot to execute its functionalities.
Sending an email is a process that can be automated. The automatable tasks include: entering your email address, password, and writing the appropriate details for your composed email. Other than obtaining the normal information, there are other things that need to be addressed.
A bot is usually written to recreate how an individual would naturally interact with some webpage or application. Therefore, we also need to account for any buttons that would be clicked while trying to send an email. In the case of Outlook, we will need to click the Next, Sign in, New message, and Send buttons.
Selenium methods needed
The four methods above make up the majority of this build.
The click() method allows you to click web elements. When writing a bot, it is imperative that we find the correct identification for the element we are trying to click. If the core element is not identified, then our bot will not work properly.
send_keys() can be used to perform key presses. We can give send_keys() arguments to press the enter, tab, and arrow keys (along with many others). Strings can also be sent to an input field by using this method. This is the primary way that send_keys() is incorporated into the email bot.
until() is called by a WebDriverWait object. WebDriverWait takes in arguments of a driver and the amount of time (in seconds) to wait for something to occur. until() is used in conjunction with certain criteria that we want to be met. There are some web elements that may not be fully loaded by the time we want to click them. You can call element_to_be_clickable(web element to click) as argument to the until method. This will make sure that we wait for a specified amount of time (determined by the number of seconds argument from the WebDriverWait object) until our web element (given as an argument to element_to_be_clickable()) becomes clickable.
The find_element_by_xpath() method allows us to set specifications for locating a particular web element. Selenium also provides methods to search for elements by id and class name. However, find_element_by_xpath() provides more freedom when it comes to specifying less common HTML elements and attributes.
I would also like to give an honorable mention to the the Chrome() and get() methods. Although these methods do not make up the main functionality of the bot, they are still worth mentioning.
Chrome() creates an instance of the Chrome web driver. Selenium uses a web driver in order to interface with the corresponding browser. Drivers need to be downloaded before executing the email bot below. Selenium offers support for many different browsers (Chrome, Firefox, Internet Explorer, etc.).
get() will create a new web browser instance. The URL provided as an argument to this method will be opened within the new browser instance.
Email bot implementation
Note: The implementation above passes an Options() object as an argument to the Chrome() method. This is used to get rid of unnecessary infobars and popup windows.
The code can be broken down as follows:
- Opens up the Outlook sign in page
- Sends your email to the email input field
- Clicks the Next button
- Sends your password to the password input field
- Clicks the Sign in button
- Clicks the New message button
- Sends the email that receives the message to the “To” input field
- Sends text to the subject input field
- Sends your message to the message input field
- Clicks the Send button
Note: driver.quit() needs to be executed regardless of whether an exception is thrown or not. The quit() method cleans up any resources being used by the web driver. If quit() is not executed, then memory will not be freed up properly and could lead to memory leaks. This method also shuts down the browser instance once it is called. Therefore, time.sleep() is needed to keep the browser open long enough to make sure that the code worked correctly.
Seeing the bot in action
In the GIF above, the email bot enters my email information, composes an email, and sends it. I show the inbox that received the bot message at the end of the GIF. The message looks completely normal (as if I had physically typed it in myself and sent it). As far as I can tell, there is no way of knowing whether the email was sent by an actual person. This realization was both exciting and terrifying at the same time. When testing the bot, I noticed something very interesting. The email that was sent only shows up in the inbox of the receiving email. If you reload the email used to send the message, then you will not see any new message sent by yourself.
Note: The email used to send the message was the Outlook email based for colleges. The normal Outlook email (not used with respect to some college) may require a different implementation than the one above. This is because some of the web elements may have slightly different values corresponding to an attribute in question.
Note: I created an unusually long video for a GIF. I also had to edit the video before converting it to a GIF. Therefore, the frame rate is extremely slow. It will take about 15 seconds or so to see the message finally come in at the end (due to the slow frame rate).
I made an email bot with minimal Selenium knowledge. I did not have to use any kind of intricate switching from one window/frame to another (this obviously depends on the email platform the bot is written for). The most advanced topic employed was calling the find_element_by_xpath() method. I honestly believe using find_element_by_xpath() gave me more freedom with respect to locating elements that contained less common attributes. It is incredible to think that an email bot can be written with only 60 lines of code (even fewer if you disregard comments and spaces). Hopefully you can use this article to build your own email bot as well!
Renaissance Engineer. (2019, May 25). Selenium WebDriver Python Tutorial - Installation and Quickstart. [YouTube video]. Renaissance Engineer. Retrieved from https://www.youtube.com/watch?v=W5GrTVwRlD4&t=8s
Renaissance Engineer. (2019, May 31). Selenium WebDriver Python Tutorial - Make A Simple Bot. [YouTube video]. Renaissance Engineer. Retrieved from https://www.youtube.com/watch?v=eZNIgKEYstQ&t=1072s
Muthukadan, B. (2018). Selenium with Python. Retrieved from https://selenium-python.readthedocs.io/
My GitHub repo for the email bot code.