# How to read OTP from email using Selenium WebDriver?

Humanless lets you read the OTP with an email API and give Selenium only the code, instead of scraping a webmail page. Create a disposable inbox over REST, type its address into your app with WebDriver, take a timestamp, submit, then call `GET /v1/identities/:id/wait`. The request stays open until the email arrives (up to 60 seconds) and returns JSON with the OTP in `code`, so there is no sleep or polling loop. It works from any language Selenium supports. In Python, `requests` is enough; a Python package is also available with `pip install humanless`.

```python test_otp.py
import os, time, requests
from selenium.webdriver.common.by import By

API = "https://api.usehumanless.com/v1"
H = {"Authorization": f"Bearer {os.environ['HUMANLESS_API_KEY']}"}

def wait_for_message(identity_id, since, deadline_s=120):
    end = time.time() + deadline_s
    while time.time() < end:
        r = requests.get(f"{API}/identities/{identity_id}/wait", headers=H, timeout=45,
                         params={"channel": "email", "timeout": 30, "since": since})
        if r.status_code == 408:  # nothing arrived during this wait: ask again
            continue
        r.raise_for_status()
        return r.json()
    raise TimeoutError("no email before the deadline")

inbox = requests.post(f"{API}/identities", headers=H, json={"channels": ["email"]}).json()
driver.find_element(By.NAME, "email").send_keys(inbox["email"])
since = int(time.time() * 1000)
driver.find_element(By.ID, "send-code").click()
driver.find_element(By.NAME, "otp").send_keys(wait_for_message(inbox["identity_id"], since)["code"])
requests.delete(f"{API}/identities/{inbox['identity_id']}", headers=H)
```

## Other options

- **IMAP**: Python's built-in `imaplib` or JavaMail/Jakarta Mail against a test mailbox, searching by recipient and polling until the message arrives.
- **The Gmail API** with plus-addressing, one `+tag` per test run.
- **Mailpit** in test environments, read with a plain HTTP call to its `/api/v1/search` endpoint.

> **Use policy:** Only for apps you own or are authorized to test. See the [Terms](/terms).

More: [Humanless REST API reference](/docs/api) · [All answers](/answers/)

Last updated 2026-12-04

Canonical: https://usehumanless.com/answers/how-to-read-otp-from-email-using-selenium-webdriver

---
More: https://usehumanless.com/llms.txt
