Building A Reddit Bot with Selenium Web Driver

Ashish Nair
Published in
3 min readOct 7, 2020

--

Hey Readers,

Hope you all are safe and healthy. This is my first blog EVER!, So I’m really excited to start, lets do that then.

So here I was surfing through the great ocean of Reddit on one fine day, as usual I came across a post that I wanted to share with my friend. As many of you Redditors know sharing posts on Reddit DM is not that convenient, you have to copy the link for the post and then open the DM window and then select the chat and then paste it and THEN send it through the Reddit DM.

Laborious isn’t it, I mean why waste time doing this, when the World needs some saving. 😜

So I decided to automate it using Selenium, what this Bot does is, it opens the upvotes section of your profile and copies the link of the latest post that you’ve upvoted and shares it in real time and it keeps refreshing to check if there are any new posts and shares them too.

Lets get started with the code then…

Requirements

You will need to have Chrome Web Driver installed for this code to work. If you don’t have it don’t worry we will go through it below.

First off, we need to Install the Python Modules we will be using.

pip install pyperclip
pip install selenium

Now, Lets get the importing part out of the way.

Lets talk about the things that I have imported first.

Selenium is a web driver it is used to automate web applications and is generally used for testing websites, it is also used for scraping websites. We will be using Selenium to open www.reddit.com and automate the sharing process.

Pyperclip is a cross-platform Python module for copy and paste clipboard functions. We will use Pyperclip to paste the link that we copy from the clipboard.

Now, Lets initialize the variables …

If you don’t have a chrome driver you can download it from here. Make sure you download the chrome driver to match the version of your chrome browser, you can check the version of your chrome driver by typing chrome://version/ on your chrome browser search bar. You will also get the chrome user data location on that page.

Snapshot of chrome://version/ page.

Now, lets create a logIn() function …

You would only need to log in once, unless you log out, as the chrome driver options argument that we added before will store the cookies so that you wouldn’t have to log in everytime you run the code.

A check() function to make sure we don’t send the same post twice.

Defining profile() function to open the profile …

The profile() takes in one parameter, the username of the person you will be sharing files to.

Feel free to change the sleep(seconds) time length and adjust it according to your requirement.

Now, We define dm() function to send the posts…

The dm() function loops through multiple tags and extracts the chat name and matches it with the username that was passed to the function and opens the chat and pastes and sends the link.

Finally, Lets call the Functions…

logIn()

Links

You can find the link to this code on my GitHub account.

Thank you for taking your time out to read my blog. It would be really great if you could leave a review in the comments section.

Make sure to HIT that follow button for more content like this.

--

--