Automating Gmail Login and Email Check

 ✅ Recommended Approach: Use Gmail API Instead

Google provides a Gmail API for safely accessing Gmail messages, sending emails, and managing inboxes. It uses OAuth 2.0 for secure authentication and is fully supported.


❌ Risky (Not Recommended): Selenium Script to Simulate Gmail Login

Example using Selenium in Python (may not work reliably due to security blocks):

from selenium import webdriver

from selenium.webdriver.common.by import By

from selenium.webdriver.common.keys import Keys

import time

# Initialize WebDriver

driver = webdriver.Chrome()

# Open Gmail

driver.get("https://accounts.google.com/signin")

# Enter Email

email_input = driver.find_element(By.ID, "identifierId")

email_input.send_keys("your_email@gmail.com")

email_input.send_keys(Keys.ENTER)

time.sleep(2)

# Enter Password

password_input = driver.find_element(By.NAME, "password")

password_input.send_keys("your_password")

password_input.send_keys(Keys.ENTER)

time.sleep(5)

# Gmail loads

driver.get("https://mail.google.com/mail/u/0/#inbox")

time.sleep(5)

# Check the subject of the latest email

emails = driver.find_elements(By.CSS_SELECTOR, "tr.zA")

if emails:

    latest_email = emails[0]

    subject = latest_email.find_element(By.CSS_SELECTOR, "span.bog").text

    print("Latest Email Subject:", subject)

else:

    print("No emails found.")

# Close browser

driver.quit()


🚀 Better Alternative: Use Gmail API

Enable Gmail API in your Google Cloud Console.

Use libraries like google-api-python-client to interact with Gmail programmatically.

Authenticate securely using OAuth 2.0 tokens.


Example snippet using Gmail API:

from googleapiclient.discovery import build

from google.oauth2.credentials import Credentials

creds = Credentials.from_authorized_user_file('token.json', ['https://www.googleapis.com/auth/gmail.readonly'])

service = build('gmail', 'v1', credentials=creds)

results = service.users().messages().list(userId='me', maxResults=1).execute()

message_id = results['messages'][0]['id']

msg = service.users().messages().get(userId='me', id=message_id).execute()

print("Subject:", next(header['value'] for header in msg['payload']['headers'] if header['name'] == 'Subject'))


🧠 Final Thoughts

While it’s tempting to use Selenium for everything, always choose secure, approved methods when dealing with email systems. For Gmail, use the Gmail API—it's safer, more reliable, and designed for automation.

Learn Selenium Python Training in Hyderabad

Read More:

Selenium with Python: Common Errors and Fixes

Using Page Object Model (POM) with Python Selenium

Automating Web Forms with Multiple Steps

Generating Test Reports with PyTest and Allure

Integrating Selenium Tests into CI/CD with Jenkins

Visit our IHub Talent Training Institute

Get Direction

Comments

Popular posts from this blog

Tosca Installation and Environment Setup

Tosca Reporting: Standard and Custom Reports

Creating Entities and Typelists in Guidewire