简体   繁体   中英

How to bypass Google reCAPTCHA for testing using Selenium

I am using Selenium to test my Spring based web application. Can you suggest a solution to bypass google reCAPTCHA while testing the application.

I am running automation test in this environment. So manually checking the "I'm not a robot" of reCAPTCHA is not possible.

For testing purpose I am using test key on my testing environment given on below location.

Google reCAPTCHA Testing Key

在此处输入图片说明

I am using Angular 5 as front-end of my application. I am using ng-recaptcha library for adding reCAPTCHA in ui.

You can do this by finding the x and y coordinates of the checkbox in reCAPTCHA and click the element.

WebElement captcha = driver.findElement(By.xpath("html/body/div[1]/div[3]/div[2]/form/div[5]/div"));
        builder.moveToElement(captcha, 50, 30).click().build().perform();

我不知道您的确切代码,但您应该能够使用系统属性或某些标志来运行您的服务器,这些标志表明应该禁用 reCaptcha 并且首先不要将其添加到表单中。

You should "switch" the driver to iFrame to locate exactly the checkbox of reCaptcha. Commands:

WebElement iFrame = driver.findElement(By.xpath("xpath_of_reCaptcha_iFrame"));
driver.switchTo().frame(iFrame);

// Now can click on checkbox of reCaptcha now.

WebElement iFrame_checkbox = 
driver.findElement(By.xpath("xpath_of_reCaptcha_checkbox"));
iFrame_checkbox.click();

I didn't find any feasible way. If you look the below code for almost all reCAPTCHA

reCAPTCHA dom

  • 1st there is iframe : You can switch to it
  • 2nd there is shadow-root element "#document", I didn't find any way to override it. I used querySelector, didn't work for me.
  • But if you pass the shadow-rootelement "#document", then you can click on the check box or here the 3rd yellow mark.

The only way is to resolve this, we have to disable it on lower environment and test it using automation.

Let me know, if anyone find out a proper way.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM