Java 如何绕过 Google reCAPTCHA 使用 Selenium 进行测试
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/50466238/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to bypass Google reCAPTCHA for testing using Selenium
提问by vivekkurien
I am using Selenium to test my Spring based web application. Can you suggest a solution to bypass google reCAPTCHA while testing the application.
我正在使用 Selenium 来测试我的基于 Spring 的 Web 应用程序。您能否提出在测试应用程序时绕过 google reCAPTCHA 的解决方案。
I am running automation test in this environment. So manually checking the "I'm not a robot" of reCAPTCHA is not possible.
我在这个环境中运行自动化测试。因此,无法手动检查 reCAPTCHA 的“我不是机器人”。
For testing purpose I am using test key on my testing environment given on below location.
出于测试目的,我在以下位置给出的测试环境中使用测试密钥。
I am using Angular 5 as front-end of my application. I am using ng-recaptcha library for adding reCAPTCHA in ui.
我使用 Angular 5 作为我的应用程序的前端。我正在使用 ng-recaptcha 库在 ui 中添加 reCAPTCHA。
采纳答案by Ido Ran
I don't know your exact code but you should be able to run your server with a System Property or some flag which indicate that the reCaptcha should be disable and to not add it to the form in the first place.
我不知道您的确切代码,但您应该能够使用系统属性或某些标志来运行您的服务器,这些标志表明应该禁用 reCaptcha 并且首先不要将其添加到表单中。
回答by Joby Wilson Mathews
You can do this by finding the x and y coordinates of the checkbox in reCAPTCHA and click the element.
您可以通过在 reCAPTCHA 中找到复选框的 x 和 y 坐标并单击该元素来完成此操作。
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();
回答by Nam Nguyen
You should "switch" the driver to iFrame to locate exactly the checkbox of reCaptcha. Commands:
您应该将驱动程序“切换”到 iFrame 以准确定位 reCaptcha 的复选框。命令:
WebElement iFrame = driver.findElement(By.xpath("xpath_of_reCaptcha_iFrame"));
driver.switchTo().frame(iFrame);
// Now can click on checkbox of reCaptcha now.
// 现在可以点击 reCaptcha 的复选框了。
WebElement iFrame_checkbox =
driver.findElement(By.xpath("xpath_of_reCaptcha_checkbox"));
iFrame_checkbox.click();