How to Automatically Login on Twitter using Selenium Webdriver

How to Automatically Login on Twitter using Selenium Webdriver – Python

Table of Contents

Selenium is a popular open-source automation testing tool for web applications. The tool is mostly used for checking to see whether web applications or codes, in general, would perform as expected. Selenium can be used for various tasks from opening a page, filling forms, clicking on the link, and many more. 

In this tutorial, you will learn how to log in to Twitter using Selenium. By the end of this article, you will learn how to write codes that can automatically visit any web page, fill forms and click on buttons. 

Without further ado, let’s jump into it. 

How to log in to Twitter with Selenium

Step 1:

The first step is to install selenium on your machine if you don’t have it. You can open your command prompt and type the command below. 

pip install selenium 

Once you click on enter, the library would be installed on your machine.

Step 2:

To use Selenium with your internet browser, you’d need to install a Webdriver Manager. There are various managers for each web browser. But rather than downloading it individually, you can install the library that has the web drivers for all web browsers. You did this by typing the following command on your command prompt

pip install webdriver_manager

Upon pressing enter, the web driver manager library would be installed. Now you’re set to write your Python code. 

You start by importing the necessary libraries. 

#import necessary libraries
from selenium import webdriver 
from selenium.webdriver.common.keys import Keys 
from webdriver_manager.chrome import ChromeDriverManager

Step 3:

You’d need to instantiate the Chrome web driver from selenium. Pass the Chrome Driver Manager as an argument and install the same. This will ensure that selenium opens Chrome and interfaces successfully with the Chrome Browser on your device. After chrome is opened, you can maximize the window using the maximaize_window() method.  

You can do all these by typing the code shown below.

#instantiate the Chrome class web driver and pass the Chrome Driver Manager
driver = webdriver.Chrome(ChromeDriverManager().install())
 
#Maximize the Chrome window to full-screen
driver.maximize_window() 

Step 4:

Define the webpage you want chrome to visit. In our case, Twitter homepage. This is what Twitter’s homepage looks like. The selenium driver can visit this page by using the get() method.

How to Automatically Login on Twitter using Selenium Webdriver - Python
#go to Twitter's Homepage
driver.get("https://twitter.com/")

Step 5:

We need the driver to click on the login button. To do this, you will need to inspect the page and find the div tag that contains the login button. Once that is found, right-click the div tag and copy Xpath. 

How to Automatically Login on Twitter using Selenium Webdriver - Python

On your IDE, call the find_by_xpath() method and pass the Xpath you copy as a string argument. Then call the click() method.

#click on the Login button
driver.find_element_by_xpath('//*[@id="react-root"]/div/div/div/main/div/div/div/div[1]/div/div[3]/a[2]/div').click()

Step 6:

This is what the login page of Twitter looks like.

How to Automatically Login on Twitter using Selenium Webdriver - Python

The driver needs to type in the email and password. Then click on login. As done earlier, you copy the Xpath of the div tag containing the email and password. Afterward, head over to your IDE and call the find_by_xpath() yet on your driver. Since we wish to type and not click this time, the send_keys() method is called rather than the click() method. You can pass your email and password for each respectively.

Finally, we repeat the same process for the login div tag and use the click() method since we want the button to be clicked.

#enter your email
driver.find_element_by_xpath('//*[@id="react-root"]/div/div/div[2]/main/div/div/div[2]/form/div/div[1]/label/div/div[2]/div/input').send_keys('[email protected]') 
  
# enter your password
driver.find_element_by_xpath('//*[@id="react-root"]/div/div/div[2]/main/div/div/div[2]/form/div/div[2]/label/div/div[2]/div/input').send_keys('Abc123.') 
  
# click on the click button  
driver.find_element_by_xpath('//*[@id="react-root"]/div/div/div[2]/main/div/div/div[2]/form/div/div[3]/div/div/span/span').click()

The entire code for the program is shown below.

#import necessary libraries
from selenium import webdriver 
from selenium.webdriver.common.keys import Keys 
from webdriver_manager.chrome import ChromeDriverManager
 
#instantiate the Chrome class web driver and pass the Chrome Driver Manager
driver = webdriver.Chrome(ChromeDriverManager().install())
 
#Maximize the Chrome window to full-screen
driver.maximize_window() 
 
#go to Twitter's Homepage
driver.get("https://twitter.com/") 
 
#click on the Login button
driver.find_element_by_xpath('//*[@id="react-root"]/div/div/div/main/div/div/div/div[1]/div/div[3]/a[2]/div').click()
 
#enter your email
driver.find_element_by_xpath('//*[@id="react-root"]/div/div/div[2]/main/div/div/div[2]/form/div/div[1]/label/div/div[2]/div/input').send_keys('[email protected]') 
  
# enter your password
driver.find_element_by_xpath('//*[@id="react-root"]/div/div/div[2]/main/div/div/div[2]/form/div/div[2]/label/div/div[2]/div/input').send_keys('Abc123.') 
  
# click on the click button  
driver.find_element_by_xpath('//*[@id="react-root"]/div/div/div[2]/main/div/div/div[2]/form/div/div[3]/div/div/span/span').click()

Once you run this, your Chrome web browser will be opened. It would automatically go to Twitter, click on the login button, fill in the login details you inserted, and login in. All by itself. 

And that’s how to login to Twitter with Selenium. If you’ve got any questions, feel free to leave them in the comment section and I’d do my best to answer them.

2 Responses

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Share this article
Subscribe
By pressing the Subscribe button, you confirm that you have read our Privacy Policy.
Need a Free Demo Class?
Join H2K Infosys IT Online Training
Enroll Free demo class