You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Selenium自动化测试中绕过Google reCAPTCHA(Spring+Angular5环境)

Bypassing Google reCAPTCHA in Selenium Tests for Spring/Angular Apps

Great question—since you’re already leveraging Google’s official test keys, you’re on the right path! These keys are specifically designed to eliminate manual reCAPTCHA interactions in testing environments, so let’s make sure you’re set up correctly with your Angular 5 + ng-recaptcha stack and Selenium tests.

Step 1: Confirm Test Key Configuration in ng-recaptcha

First, double-check that your frontend uses the test site key, and your Spring backend uses the matching secret key:

  • Test Site Key (frontend): 6LeIxAcTAAAAAJcZVRqyHh71UMIEGNQ_MXjiZKhI
  • Test Secret Key (backend): 6LeIxAcTAAAAAGG-vFI1TnRWxMZNFuojJ4WifJWe

For ng-recaptcha, the cleanest approach is to use environment-specific configs. Update your environment.test.ts:

export const environment = {
  production: false,
  recaptchaSiteKey: '6LeIxAcTAAAAAJcZVRqyHh71UMIEGNQ_MXjiZKhI'
};

Bind this value in your component template:

<re-captcha [siteKey]="environment.recaptchaSiteKey"></re-captcha>

If you prefer setting it at the module level, add the provider to AppModule:

import { RECAPTCHA_SETTINGS, RecaptchaSettings } from 'ng-recaptcha';

@NgModule({
  providers: [
    {
      provide: RECAPTCHA_SETTINGS,
      useValue: { siteKey: environment.recaptchaSiteKey } as RecaptchaSettings
    }
  ]
})
export class AppModule {}

Step 2: Automatically Retrieve the Valid Token in Selenium

With the test key, reCAPTCHA generates a valid token without any user action. You can fetch this token directly via Selenium’s JavaScript executor—no manual clicks needed.

Example (Java Selenium):

// Wait for the reCAPTCHA widget to fully load
WebDriverWait wait = new WebDriverWait(driver, 10);
wait.until(ExpectedConditions.presenceOfElementLocated(By.cssSelector("div.g-recaptcha")));

// Execute JS to get the response token
String recaptchaToken = (String) ((JavascriptExecutor) driver).executeScript(
    "return grecaptcha.getResponse();"
);

Alternative for ng-recaptcha (if the above fails):

Sometimes ng-recaptcha wraps the widget instance differently. Try targeting the component’s template reference:

// If your template has <re-captcha #captcha ...></re-captcha>
String recaptchaToken = (String) ((JavascriptExecutor) driver).executeScript(
    "return document.querySelector('#captcha').recaptchaInstance.getResponse();"
);

Fallback: Manually Inject the Token

If the widget doesn’t auto-generate the token (rare with test keys), you can directly set it in the hidden input ng-recaptcha uses to send the token to your backend:

WebElement responseInput = driver.findElement(By.name("g-recaptcha-response"));
((JavascriptExecutor) driver).executeScript(
    "arguments[0].value = 'test-token-valid-123';", responseInput
);

Note: With test keys, any string will be accepted by the backend as long as you’re using the matching secret key.

Step 3: Update Spring Backend for Test Environment

Don’t forget to switch your Spring app to use the test secret key in test mode. Add this to your application-test.properties:

recaptcha.secret-key=6LeIxAcTAAAAAGG-vFI1TnRWxMZNFuojJ4WifJWe

Then, in your reCAPTCHA verification service, pull this property instead of the production key.

Troubleshooting Common Issues

  • Widget not loading: Check the browser console for reCAPTCHA errors—ensure the site key is correctly passed to ng-recaptcha.
  • Token not found: Use explicit waits (like WebDriverWait) instead of hard sleeps to ensure the widget is fully loaded before fetching the token.
  • Backend verification fails: Double-check that the secret key matches the test site key, and that your service sends the correct token to Google’s verification endpoint.

Remember: These test keys are strictly for testing—never deploy them to production. They’re built to let your automated tests run smoothly without needing to solve reCAPTCHA challenges manually.

内容的提问来源于stack exchange,提问作者vivekkurien

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.28 09:25:40