Skip to content

Instantly share code, notes, and snippets.

@Dema
Created October 21, 2019 20:08
Show Gist options
  • Save Dema/dcc2b4f91b97ec9d56afc871e66fd3d7 to your computer and use it in GitHub Desktop.
Save Dema/dcc2b4f91b97ec9d56afc871e66fd3d7 to your computer and use it in GitHub Desktop.
Cypress recaptcha command
Cypress.Commands.add("clickRecaptcha", () => {
cy.window().then(win => {
win.document
.querySelector("iframe[src*='recaptcha']")
.contentDocument.getElementById("recaptcha-token")
.click();
});
});
@andrestesterqa
Copy link

This does not work for this type of captcha image

Any solution for that? Please

@Antoniossss
Copy link

Antoniossss commented Jul 22, 2022

Why are you guys keep asking how to pass automation blocking software using automation software? It is meant to not to be passed like that by design and byt its solely purpose

@zohidweb
Copy link

  1. For reCAPTCHA v2, use the following test key:
    const globalSettings: RecaptchaSettings = {siteKey: '6LeIxAcTAAAAAJcZVRqyHh71UMIEGNQ_MXjiZKhI'};

  2. In cypress.config.ts file set chromeWebSecurity: false

  3. Click recaptcha:

 cy.get('iframe[src*=recaptcha]')
      .its('0.contentDocument')
      .should(d => d.getElementById('recaptcha-token').click())

@theSalafee
Copy link

theSalafee commented Nov 7, 2023

Here's my take (reCAPTCHA v2), which doesn't require waiting:

cy.get('iframe[src*=recaptcha]')
        .its('0.contentDocument')
        .should(d => d.getElementById('recaptcha-token').click())

This is interesting. I was able to get it to work as well as the following code here:

        cy.get('iframe[src*=recaptcha]').then(($iframe) => {
            const iframeDocument = $iframe.contents();
            const recaptchaToken = iframeDocument.find('#recaptcha-token');
            recaptchaToken.click();
        });

However, the next day after a few tests it would not work and was not able to find the #recaptcha-token or the .its() would fail. This is what my team was seeing while reviewing a PR I'd created. I had set up the chromewebsecurity to false and was using the correct test keys provided by Google.

I ended up spending the entire day troubleshooting and trying to figure out what was happening. After a lot of frustrations, I hard reloaded and emptied the cache and now both versions of the click work!!! I have no clue why or what is going on frankly.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment