</>

Technology

Scenario-Based

Difficulty

Advanced

Interview Question

How do you handle CAPTCHA in Selenium automation?

Answer

Handling CAPTCHA in Selenium Automation

CAPTCHA is intentionally designed to block automation. There is no clean Selenium-only solution — these are the real approaches used in professional QA:

Approach 1 — Disable CAPTCHA in Test Environment (Best)

Ask developers to disable CAPTCHA for the staging/test environment:

Java
// Dev adds a feature flag or test bypass
driver.get("https://staging.automateqa.online/login?captcha=disabled");

// Or environment variable in app config
// DISABLE_CAPTCHA=true (staging only, never production)

This is the correct enterprise approach. Your CI/CD environment should never have CAPTCHA enabled.

Approach 2 — Backend Bypass via API

Skip the UI entirely for login — get a token via API:

Java
// Use Rest Assured to get auth token directly (bypasses CAPTCHA UI)
Response response = RestAssured
    .given()
    .contentType("application/json")
    .body("{\"email\":\"admin@test.com\",\"password\":\"secret\"}")
    .post("https://api.automateqa.online/auth/login");

String token = response.jsonPath().getString("token");

// Inject token as cookie into browser
driver.get("https://automateqa.online");
driver.manage().addCookie(new Cookie("auth_token", token));
driver.navigate().refresh(); // now logged in, CAPTCHA bypassed

Approach 3 — Third-Party CAPTCHA Solving Services

For cases where CAPTCHA cannot be disabled (audits, production-like tests):

Java
// 2captcha API example (paid service ~$0.001 per CAPTCHA)
// 1. Get the sitekey from page source
String siteKey = driver.findElement(By.cssSelector(".g-recaptcha"))
                       .getAttribute("data-sitekey");

// 2. Send to 2captcha API
String requestUrl = "http://2captcha.com/in.php?key=YOUR_API_KEY"
    + "&method=userrecaptcha&googlekey=" + siteKey
    + "&pageurl=" + driver.getCurrentUrl();
// 3. Poll for result, inject into form
// 4. Submit form with solved token

Note: This adds cost + latency and violates most sites'' terms of service. Use only for authorized penetration testing.

Approach 4 — Image-Based CAPTCHA (Optical — Avoid)

Using Tesseract OCR for simple text CAPTCHAs:

Java
// NOT recommended — modern CAPTCHAs defeat OCR
// Only works for very simple legacy CAPTCHAs

Approach 5 — Mock/Stub CAPTCHA Validation

If you control the backend, accept a test bypass token:

Java
// Backend: if captcha_token == "test-bypass-token-xyz", skip validation
// Frontend automation:
driver.findElement(By.id("captcha_token"))
      .sendKeys("test-bypass-token-xyz");

Summary: The Right Approach for Each Situation

SituationApproach
Staging/CI environmentDisable CAPTCHA via feature flag
Login automationAPI token injection into cookies
Must test CAPTCHA pageVerify CAPTCHA element exists, skip solving
Production auditThird-party solving service (authorized only)

The best answer in an interview: CAPTCHA should be disabled in test environments via a dev-provided flag. If not possible, bypass login via API token and inject as a cookie.

Follow AutomateQA

Related Topics