Gmail Login using Python Selenium
Last Updated :
22 Mar, 2024
Python scripting is one of the most intriguing and fascinating things to do meanwhile learning Python. Automation and controlling the browser is one of them.
In this particular article, we will see how to log in to the Gmail account using Python and the power of selenium.
Selenium automates and controls browsers and it’s activity. We can code in our way to control browser tasks with the help of selenium. Primarily, it is for automating web applications for testing purposes, but is certainly not limited to just that. Boring web-based administration tasks can be automated as well. As you learn more it’s so much fun to see things happening automatically and saving time in doing useless tasks again and again.
We use selenium here to open the site of our requirement (in this case Gmail) and there we inspect elements across email box, password box, and Next button to find the Xpath of them.
- Using find_elemenT(By.XPATH) function provided by selenium module, we can find the required element (username box, password box, Next button)
- Using send_keys() function, provided by selenium module, we will send the data into the box.
- Using click() function, provided selenium module, we make a click on the current element.
- .get() attribute allows us to open a webpage
- .implicitly_wait() allows us to wait till the page loads
Installing third party modules required
- Selenium
- getpass
- Additional Requirement: geckodriver for firefox and chromedriver for chrome
Importing necessary modules
Selenium : to automate browser
Taking username and password as input from user
Using input() function and passing prompt message as argument.
Opening browser and required website
webdriver.Chrome() will open new window of chrome. We will save it’s object in variable named driver.
Now using get function we will open up the Gmail website.
Finding element for sending data and Sending input
Use inspect element tool on the element of browser of which you want to find id. In this case we will inspect username box, password box, Next button to find their Xpath. And then use this Xpath combining with selenium function find_element(By.XPATH) to find it across web page and save it in variables for later use. Then by using send_keys() we will send data across the elements found previously.then by using click() we will click the element found.
Note: Here driver is the name of variable you choose for webdriver.Chrome().
Algorithm:
This is a simple program that can login to your gmail account. Best case the account is not secure or an organisation mail-id. Worst case if you have a gmail account that is enabled two-step or two-factor authentication. Algorithm: Step 1: Get the mail-id and password Step 2: Open the gmail login page Step 3: Enter the gmail-id and click next button Step 4: Enter the password and click next button Step 5: Print success or failed and End
Complete Code:
python3
from selenium import webdriver
from webdriver_manager.chrome import ChromeDriverManager
from selenium.webdriver.common.by import By
print('Enter the gmailid and password')
gmailId, passWord = map(str, input().split())
try:
driver = webdriver.Chrome(ChromeDriverManager().install())
driver.get(r'https://accounts.google.com/signin/v2/identifier?continue='+\
'https%3A%2F%2Fmail.google.com%2Fmail%2F&service=mail&sacu=1&rip=1'+\
'&flowName=GlifWebSignIn&flowEntry = ServiceLogin')
driver.implicitly_wait(15)
loginBox = driver.find_element(By.XPATH,'//*[@id ="identifierId"]')
loginBox.send_keys(gmailId)
nextButton = driver.find_elements(By.XPATH,'//*[@id ="identifierNext"]')
nextButton[0].click()
passWordBox = driver.find_element(
By.XPATH,'//*[@id ="password"]/div[1]/div / div[1]/input')
passWordBox.send_keys(passWord)
nextButton = driver.find_elements(By.XPATH,'//*[@id ="passwordNext"]')
nextButton[0].click()
print('Login Successful...!!')
except:
print('Login Failed')
See how such a concise piece of code can automate things for you.
Bonus:
We can also enter the password without displaying it on screen, for security purpose. For that we have to include one more module called getpass. Now with just one change in input statement of the password we can input password without displaying it on screen.
python3
from getpass import getpass
pwd = getpass('Enter Password:')
Getpass prompts the user for a password without echoing. Basically, it lets you enter the password without showing it on the screen.
Similarly you can also automate many other things like twitter login, tweeting, Gmail logout, and much more.
Similar Reads
Gmail Login using Java Selenium
Automating Gmail login using Selenium WebDriver and Java is a common task for beginners learning web automation. Itâs an excellent exercise to understand how Selenium interacts with web elements and handles user inputs. This article will guide you through the process of automating Gmail login, provi
4 min read
Login Twitter using Python Selenium
Project Description:- Here, we're going to study a simple how-to log in twitter by selenium. Selenium is a free tool for automated trying out across exceptional browsers. Requirement: Selenium â Selenium Python Introduction and Installation Below are the steps: First, go to the twitter website using
2 min read
Python - Opening links using Selenium
Selenium is a powerful tool for controlling the web browser through the program. It is functional for all browsers, works on all major OS and its scripts are written in various languages i.e Python, Java, C#, etc, we will be working with Python. Selenium Python bindings provide a convenient API to a
2 min read
Download File in Selenium Using Python
Prerequisite: Selenium Selenium is a powerful tool for controlling web browsers through programs and performing browser automation. It is functional for all browsers, works on all major OS and its scripts are written in various languages i.e. Python, Java, C#, etc. We will be working with Python. Se
2 min read
Python - Opening multiple tabs using Selenium
Testing is an important concept in software methodology. Software is said to be effective and efficient only if it is bug-free. Testing can be done manually and also via automation. In Python, selenium is used to do automated testing. The selenium package is available, and they are much helpful to a
4 min read
Automating Google meet using selenium in Python
Prerequisites: Selenium Python basics, Browser Automation using Selenium Selenium is a powerful tool for controlling web browsers through programs and performing browser automation. It is functional for all browsers, works on all major OS and its scripts are written in various languages i.e Python,
3 min read
Locating single elements in Selenium Python
Locators Strategies in Selenium Python are methods that are used to locate elements from the page and perform an operation on the same. Seleniumâs Python Module is built to perform automated testing with Python. Selenium Python bindings provide a simple API to write functional/acceptance tests using
5 min read
Search Google Using Python Selenium
Selenium's Python Module is built to perform automated testing with Python. Selenium Python bindings provides a simple API to write functional/acceptance tests using Selenium WebDriver. Through Selenium Python API you can access all functionalities of Selenium WebDriver in an intuitive way. This art
1 min read
Send Emails Using Python
By using Python, you can send emails which can be a valuable skill for automation, communication, and data-driven processes. In this article, we will explore how to send mail from Gmail using Python. How can you send Emails using Python?Python offers a library to send emails- "SMTP" Library. "smtpli
4 min read
Twitter Automation using Selenium Python
If you are someone like me who considers Twitter to be far better than Instagram, then I might be having something for you. We all know gaining followers on twitter can be pretty tough but sometimes retweeting quality content can gain you, followers, too. Obviously, you are a busy person and you don
6 min read