How I built a Twitter Bot using Python and Selenium?

Let your Python script do all the tweeting!!

Vishal Sharma
Towards Data Science

--

Twitter Bots using Python
Twitter Bots using Python — Twilio

We are seeking automation in every single thing on this planet. From form autofill to self-driving cars, humans have come a long way using automated processes. Python scripts come very handy in generating automated tools and tasks. On the other hand, Selenium is known for automating browser and web apps. When the forces of Python and Selenium combine, there are hundreds of tasks one can automate with ease. Thanks to the combination of these two, making bots have become easier.

This article is a guide to make a simple Twitter bot that will tweet for you itself.

Libraries

import time
from selenium import webdriver
from selenium.webdriver.common.keys import Keys

Selenium is the main library we will require to build our Twitter bot. I have imported “webdriver” subpackage from selenium to access the browser and perform tasks on it. Further, I have imported the “Keys” subpackage to give input with my keyboard keys in the automation process.

Now comes the Meat Part

I have initialized a class “TwitterBot” having a constructor that takes username and password of the Twitter account. Also, it kicks the Chrome web driver for automation.

class TwitterBot():
def __init__(self,username,password):
self.browser=webdriver.Chrome("/Users/vishalsharma/Downloads/chromedriver")
self.username=username
self.password=password

You can download Chrome web driver from here

I have defined a function “SignIn” inside the class that logs into the twitter handle by inputting username and password in the login form. I have used “find_element_by_name” method to locate the web element and fill the form.

def signIn(self):
self.browser.get("https://www.twitter.com/login")
time.sleep(5)
usernameInput=self.browser.find_element_by_name("session[username_or_email]")
passwordInput=self.browser.find_element_by_name("session[password]")
usernameInput.send_keys(self.username)
passwordInput.send_keys(self.password)
passwordInput.send_keys(Keys.ENTER)
time.sleep(5)

For our tweeting segment, I have used the “find_element_by_xpath” method and tweeted using the “send_keys” method. In the last statement, “send_keys” have two parameters. These two parameters press the Command+Enter key to post the tweet.

def TweetSomething(self):
tweet = self.browser.find_element_by_xpath('''//*[@id='react-root']/div/div/div[2]/main/div/div/div/div/div
/div[2]/div/div[2]/div[1]/div/div/div/div[2]/div[1]/div/div
/div/div/div/div/div/div/div/div[1]/div/div/div/div[2]/div
/div/div/div''')
tweet.send_keys("""Hello World!""")
tweet.send_keys(Keys.COMMAND, Keys.ENTER)

In the “find_element_by_xpath”, you might find loads of gibberish. But, it is just a way to locate the web element. You can also use other locating methods too. Find them here!

Calling the class

Now, give the username and password in the console. Call the “TwitterBot “class and run the methods in the class.

if __name__=="__main__":
username= input("Enter your username: ")
password= input("Enter your password: ")
t=TwitterBot(username,password)
t.signIn()
t.TweetSomething()

And that’s it! You have built a Twitter bot that tweets for you. Now, you can check Instagram followers, like the photos, and even right swipe at your Tinder profile. Now, go on and try automating your social media using Python and Selenium.

For discussions and feedbacks, find me at Linkedin!

--

--