Creating Gmail accounts manually can be tedious, especially if you need multiple accounts for testing or other purposes. In this tutorial, you’ll learn how to automate the Gmail account registration process using Python and Selenium. We’ll also use the faker
library to generate random user data and random
for password generation.
Prerequisites
Before we begin, ensure you have the following installed:
- Python 3.x
- Selenium
- Chrome WebDriver
- Faker
You can install Selenium and Faker using pip:
pip install selenium faker
Download the Chrome WebDriver from here and make sure it is in your PATH.
Script Overview
The script will:
- Generate random first name, last name, username, and password.
- Open the Gmail signup page using Selenium.
- Fill out the signup form with the generated data.
- Submit the form.
Step 1: Import Libraries and Initialize Faker
First, we’ll import the necessary libraries and initialize Faker
:
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from faker import Faker
import random
import string
import time
# Initialize Faker
fake = Faker()
Step 2: Generate Random Data
Next, we’ll create a function to generate a random password and generate the random user data:
# Function to generate a random password
def generate_password(length=12):
characters = string.ascii_letters + string.digits + string.punctuation
password = ''.join(random.choice(characters) for i in range(length))
return password
# Generate random user data
first_name = fake.first_name()
last_name = fake.last_name()
username = first_name.lower() + last_name.lower() + str(random.randint(1000, 9999))
password = generate_password()
Step 3: Initialize Selenium WebDriver
Initialize the Chrome WebDriver and navigate to the Gmail signup page:
# Initialize Chrome WebDriver
driver = webdriver.Chrome()
# Open Gmail signup page
driver.get("https://accounts.google.com/signup")
Step 4: Fill Out the Signup Form
Wait for the page to load, locate the form fields, and fill them out with the generated data:
# Wait for the page to load and locate the form fields
wait = WebDriverWait(driver, 10)
first_name_field = wait.until(EC.presence_of_element_located((By.ID, "firstName")))
last_name_field = driver.find_element(By.ID, "lastName")
username_field = driver.find_element(By.ID, "username")
password_field = driver.find_element(By.NAME, "Passwd")
confirm_password_field = driver.find_element(By.NAME, "ConfirmPasswd")
# Fill out the form fields
first_name_field.send_keys(first_name)
last_name_field.send_keys(last_name)
username_field.send_keys(username)
password_field.send_keys(password)
confirm_password_field.send_keys(password)
Step 5: Submit the Form
Submit the form and add a delay to allow the next page to load:
# Submit the form
next_button = driver.find_element(By.XPATH, '//*[@id="accountDetailsNext"]/div/button')
next_button.click()
# Add a delay to allow the next page to load (you may need to adjust the sleep time)
time.sleep(5)
Step 6: Handle Additional Steps
Depending on Google’s signup process, you may need to handle additional steps like phone verification, recovery email, or CAPTCHA. This part will vary and may require more advanced handling.
Step 7: Close the Browser
Close the browser once the process is complete:
# Close the browser after the process is complete
driver.quit()
# Output the generated data
print(f"First Name: {first_name}")
print(f"Last Name: {last_name}")
print(f"Username: {username}")
print(f"Password: {password}")
Full Script
Here is the full script combining all the steps:
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from faker import Faker
import random
import string
import time
# Initialize Faker
fake = Faker()
# Function to generate a random password
def generate_password(length=12):
characters = string.ascii_letters + string.digits + string.punctuation
password = ''.join(random.choice(characters) for i in range(length))
return password
# Generate random user data
first_name = fake.first_name()
last_name = fake.last_name()
username = first_name.lower() + last_name.lower() + str(random.randint(1000, 9999))
password = generate_password()
# Initialize Chrome WebDriver
driver = webdriver.Chrome()
# Open Gmail signup page
driver.get("https://accounts.google.com/signup")
# Wait for the page to load and locate the form fields
wait = WebDriverWait(driver, 10)
first_name_field = wait.until(EC.presence_of_element_located((By.ID, "firstName")))
last_name_field = driver.find_element(By.ID, "lastName")
username_field = driver.find_element(By.ID, "username")
password_field = driver.find_element(By.NAME, "Passwd")
confirm_password_field = driver.find_element(By.NAME, "ConfirmPasswd")
# Fill out the form fields
first_name_field.send_keys(first_name)
last_name_field.send_keys(last_name)
username_field.send_keys(username)
password_field.send_keys(password)
confirm_password_field.send_keys(password)
# Submit the form
next_button = driver.find_element(By.XPATH, '//*[@id="accountDetailsNext"]/div/button')
next_button.click()
# Add a delay to allow the next page to load (you may need to adjust the sleep time)
time.sleep(5)
# Close the browser after the process is complete
driver.quit()
# Output the generated data
print(f"First Name: {first_name}")
print(f"Last Name: {last_name}")
print(f"Username: {username}")
print(f"Password: {password}")
Conclusion
You’ve successfully automated the Gmail account registration process using Python and Selenium. This script generates random user data and fills out the form automatically. Remember to use this script responsibly and be aware of the legal and ethical implications of automating account creation.