1. 简介
自动化测试是持续集成的一个关键方面,在处理验证码等安全措施时经常会遇到挑战。对持续和重复测试运行的需求可能会触发安全机制,如Cloudflare验证码,从而导致测试失败。虽然有可能在测试执行期间禁用验证码,但在某些情况下这可能并不可行。在这篇博客文章中,我们深入探讨了Cloudflare验证码的世界,重点关注独立Cloudflare Turnstile验证码和Cloudflare Turnstile挑战页面。我们将探索这些验证码的复杂性,并讨论以自动化方式绕过它们的两种有效方法。
2. 了解Cloudflare验证码
- Cloudflare turnstile验证码有2种类型:
- 独立Cloudflare Trunstile验证码:验证码小部件放在网站页面上,保护表单免受自动提交影响。[参见此示例。](https://2captcha.com/demo/cloudflare-turnstile)
- Cloudflare Turnstile挑战页面:网站上的验证码通过Cloudflare代理。[参见此示例](https://rucaptcha.com/42)。
3. 旁路技术
3.1. 免费(仅限Python):
- [未检测到的Chrome驱动程序](https://github.com/ultrafunkamsterdam/undetected-chromedriver):利用未检测到的Chrome浏览器允许自动化脚本与网站进行交互,而不会触发Cloudflare的安全检查。然而,该库仍然被最新的Cloudflare算法检测到。
- [请在此处查看Python的示例文件](https://github.com/ultrafunkamsterdam/undetected-chromedriver/blob/master/example/test_workflow.py)。
```
import undetected_chromedriver as uc
driver = uc.Chrome(headless=True,use_subprocess=False)
driver.get('https://rucaptcha.com/42')
driver.save_screenshot('captcha.png')
```
- [DrissionPage](https://github.com/g1879/DrissionPage):DrissionPage是一个Python库,能够自动进行浏览器交互,可用于在不被检测的情况下浏览受Cloudflare保护的网站。
- [请在此处查看DrissionPage文档](https://g1879.gitee.io/drissionpagedocs/SessionPge/introduction/)。
```
from DrissionPage import ChromiumPage
page = ChromiumPage()
page.get('https://rucaptcha.com/42')
```
3.2. 使用2Captcha付费旁路:
**它是如何工作的?**
向2Captcha发送API以接收cf-turnstile-response令牌。他们的工作人员将手动绕过这个验证码,并将cf-turnstile-response令牌发回给我们。最后,通过设置cf-turnstile-response输入的值,使用此令牌绕过Cloudflare。
3.2.1. 独立Turnstile验证码
步骤1:向2Captcha发送turnstile旁路请求,内容如下:
- 端点:[https://2captcha.com/in.php](https://2captcha.com/in.php**)
- 方法:**POST**
- 请求正文为JSON:
```
{
"key": "YOUR_API_KEY", // Go to https://2captcha.com/enterpage and cop