Start Hacking Your Life with Selenium WebDriver and Python
Now that I’m halfway through my time at the Flatiron School, I’ve become accustomed to the rhythm of each three week phase (neé module). At the beginning of each phase, we learn a new language or framework, then we’re evaluated on our understanding of it in a code challenge, and finally project week with student blog presentations. A while ago, some people in my cohort mentioned it would be helpful for everyone to like each others’ posts to boost engagement, and so this blog idea was born.
In this blog, I’ll be walking you through the beginning of building a bot to follow a Medium author, and like and comment on all of their posts given the author’s username.
Prerequisites
Start by installing Python and Selenium (along with the appropriate drivers) on your machine. Note that, if you choose to use Safari, you should also select Allow Remote Automation in the Developers menu.
Project Set-up
In the spirit of saving time, I first did a quick search on Github for similar projects. I was open to building this bot in Javascript to get more practice, but decided to fork from this Github repository instead for the following reasons: 1) I‘m already familiar with Python, 2) there is very similar functionality in the code to what I imagined for the final product, and 3) I thought those that are less familiar with Python would appreciate the GUI.
Upon forking the repo, I followed an error-driven approach to install a few more packages to get the program running as the original repository had not been updated in a few years.
Now, it was time to cipher through all the helper functions to see what i had to add for my desired functionality.
After a quick perusal, I noticed I could reuse the majority of the code. My goal was then to create a helper function that would prompt for a target user, scrape the urls of all the user’s posts, and then boost engagement by following the user, liking all the posts, and commenting at random.
To do that, I took this:
and modified it based off the HTML structure of a user’s test page to have the functionality mentioned above…
Now, I could insert my new functions where where NavigateToURLAndScrapeRelatedTags was previously called.
Bugs
In a perfect world, this is all I would need to do. However, the HTML of Medium has changed over the last couple of years. As many developers know, the majority of my time was spent debugging. I had to evaluate the HTML page source and usually copy the new XPath of many elements mentioned in the code for full navigation.
I was able to restore most of the functionality, however, at the time of publishing, the bot still is having trouble liking posts with the “clap” button. 🥺😭😭😭😭
Demo
Look mom! No hands!