# OTP functionality with Selenium WebDriver

Humanless gets the OTP for your Selenium WebDriver test from an API, since Selenium drives the browser but can't receive the code; type it with `sendKeys`. The test creates a disposable inbox (`POST /v1/identities`), enters that address in your app, records the time, triggers the OTP email, and calls `GET /v1/identities/:id/wait`, which blocks until the email lands and returns the code already parsed. The example uses Java's built-in `HttpClient` and Jackson; any HTTP client works. Delete the identity at the end with `DELETE /v1/identities/:id`. Use a new identity per test so parallel runs never read each other's codes. Pay-as-you-go accounts can create 30 identities per hour (Starter 100, Team 300, Scale 1,000), so for larger suites reuse an identity across a spec or save the login state.

```java OtpTest.java
HttpClient http = HttpClient.newHttpClient();
ObjectMapper json = new ObjectMapper();
String api = "https://api.usehumanless.com/v1/identities";
String auth = "Bearer " + System.getenv("HUMANLESS_API_KEY");

JsonNode inbox = json.readTree(http.send(HttpRequest.newBuilder(URI.create(api))
        .header("Authorization", auth).header("Content-Type", "application/json")
        .POST(HttpRequest.BodyPublishers.ofString("{\"channels\":[\"email\"]}")).build(),
    HttpResponse.BodyHandlers.ofString()).body());
driver.findElement(By.name("email")).sendKeys(inbox.get("email").asText());
long since = System.currentTimeMillis();
driver.findElement(By.id("send-code")).click();

URI wait = URI.create(api + "/" + inbox.get("identity_id").asText() + "/wait?channel=email&timeout=30&since=" + since);
long deadline = System.currentTimeMillis() + 120_000;
HttpResponse<String> res;
do {  // 408 means nothing arrived during this wait: ask again until the deadline
  res = http.send(HttpRequest.newBuilder(wait).header("Authorization", auth).build(),
      HttpResponse.BodyHandlers.ofString());
} while (res.statusCode() == 408 && System.currentTimeMillis() < deadline);
if (res.statusCode() != 200) throw new IllegalStateException("No OTP email: " + res.body());
driver.findElement(By.name("otp")).sendKeys(json.readTree(res.body()).get("code").asText());
```

## Other options

- **JavaMail / Jakarta Mail over IMAP** against a test mailbox, polling with a search on the recipient.
- **A test-mode OTP** accepted only for seeded test users outside production.
- **Mailpit** as the SMTP server for test environments, read over HTTP.

> **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/otp-functionality-with-selenium-webdriver

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