Selenium Email Testing
Selenium drives the browser; for the email step you need a routable inbox your test can read. This Selenium email testing guide uses Python and a disposable inbox API — see also the Playwright and Cypress versions, all part of email testing for developers.
Inbox helpers
import os, time, requests
from selenium import webdriver
from selenium.webdriver.common.by import By
API = "https://moemail.app/api"
H = {"X-API-Key": os.environ["MAIL_KEY"]}
def create_inbox():
return requests.post(API + "/emails/generate", headers=H).json()
def wait_for_email(inbox_id, timeout=30):
end = time.time() + timeout
while time.time() < end:
msgs = requests.get(API + "/emails/" + inbox_id + "/messages", headers=H).json()["messages"]
if msgs:
return msgs[0]
time.sleep(1.5)
raise TimeoutError("no email arrived")
The test
import re
def test_signup_verification():
inbox = create_inbox()
driver = webdriver.Chrome()
driver.get("https://example.test/signup")
driver.find_element(By.ID, "email").send_keys(inbox["address"])
driver.find_element(By.CSS_SELECTOR, "button[type=submit]").click()
msg = wait_for_email(inbox["id"])
code = re.search("[0-9]{6}", msg["text"]).group(0)
driver.find_element(By.ID, "code").send_keys(code)
assert "Welcome" in driver.page_source
driver.quit()
Use an explicit wait for page elements and the polling helper for the email. The regex [0-9]{6} grabs a six-digit code; for a link, search for the URL instead and call driver.get().
Tips
One inbox per test keeps runs isolated; a webhook beats polling for speed. Create a mailbox and check the OpenAPI docs.