How to test email verification in Selenium with Python
Selenium can fill your signup form, but most suites stop at "check your email". With a disposable inbox API, your Python test can read the email and finish the job.
Step 1: the inbox helper
This uses QuickInbox's free API, which needs no signup or key. Install requests and add:
# inbox.py - disposable inboxes from QuickInbox's free API (no key needed)
import re, time, requests
BASE = "https://quickinbox.net"
def create_inbox(minutes=10):
r = requests.post(f"{BASE}/api/generate", json={"minutes": minutes})
r.raise_for_status()
address = r.json()["address"]
return address, address.split("@")[0]
def wait_for_email(local, subject="", timeout=60, interval=2):
deadline = time.time() + timeout
while time.time() < deadline:
r = requests.get(f"{BASE}/api/inbox/{local}")
if r.ok:
for m in r.json().get("messages", []):
if subject in m["subject"]:
return m # id, from, subject, text, html, receivedAt
time.sleep(interval)
raise TimeoutError(f"No email with subject {subject!r} in {timeout}s")
def extract_code(email):
m = re.search(r"\b(\d{4,8})\b", email.get("text") or "")
if not m:
raise ValueError("No code found")
return m.group(1)
def extract_link(email, pattern):
for url in re.findall(r'href="([^"]+)"', email.get("html") or ""):
url = url.replace("&", "&")
if re.search(pattern, url):
return url
raise ValueError(f"No link matching {pattern}")Step 2: test a verification link
# test_signup.py
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
import inbox
def test_signup_and_verify():
address, local = inbox.create_inbox()
driver = webdriver.Chrome()
try:
driver.get("https://staging.example.com/signup")
driver.find_element(By.NAME, "email").send_keys(address)
driver.find_element(By.NAME, "password").send_keys("Correct-Horse-42")
driver.find_element(By.CSS_SELECTOR, "button[type=submit]").click()
email = inbox.wait_for_email(local, subject="Verify")
driver.get(inbox.extract_link(email, r"/verify"))
WebDriverWait(driver, 10).until(
EC.text_to_be_present_in_element((By.TAG_NAME, "body"), "verified"))
finally:
driver.quit()Step 3: or a one-time code
email = inbox.wait_for_email(local, subject="code")
driver.find_element(By.NAME, "code").send_keys(inbox.extract_code(email))Tips
- Give each test its own inbox, so tests can run in parallel with
pytest-xdist. - The free API allows about 10 new addresses per minute per IP. For big parallel suites, use the Business API, where any tag on your namespace is a new address with no create call.
- Never send real data to a free inbox: anyone who knows the address can read it.
- Same idea for SMS codes: SMS 2FA in Selenium.
Related guides
- Email testing in Jest and Node.js
- Email testing in Python with pytest
- How to test magic link login
- How to test email OTP and 2FA codes
- Email testing API overview
Try it now: the free endpoints above need no signup and are documented on the temporary email API page. For private inboxes, webhooks and CI at scale, the QuickInbox Business API has a free plan with 5,000 emails a month.