# 验证码解决方案

### **验证码解决方案**

\
在使用我们的采集浏览器时，难免会遇到需要解决的风险控制验证码。这些验证码可能会影响您的自动化任务。但请放心，我们将协助您解决这一问题。

### **自定义CDP功能**

\
若遇到风险控制验证码，您需要通过CDP命令通知我们。收到指令后，我们将协助完成验证码破解。您只需发送CDP命令：`Captchas.automaticSolver` 并附上需要破解的验证码类型即可。

### **自动验证码破解功能**

\
您只需在可能出现验证码的代码部分添加以下代码片段，通过CDP命令告知我们需要破解的验证码类型，我们将立即协助处理。

{% tabs %}
{% tab title="Puppeteer" %}

```sh
const page = await page.newPage();  
await page.goto('https://example.com');  
```

{% endtab %}

{% tab title="Selenium" %}

```python
res = driver.execute_cdp_cmd("Captchas.automaticSolver", {
        "timeout": 120000,   # Please allow sufficient time, and 2 minutes is recommended.
        "solverType": "cloudflare"
    })
    print(f"Captchas solver result: {res}")
```

{% endtab %}
{% endtabs %}

### **示例说明**

{% tabs %}
{% tab title="JavaScript" %}

```sh
const puppeteer = require('puppeteer-core');

const Target_URL = "https://example.com";

const wsUrl = 'wss://{AUTH}@ws-browser.thordata.com';  //Your credentials
async function run(){

  // connect browser
  const browser = await puppeteer.connect({
	  browserWSEndpoint: wsUrl,
  });
  try{
	  console.log("Connected to browser...");
	
	  // Create a new page
	  const page = await browser.newPage();

	  // Go to target site
	  await page.goto(Target_URL, { waitUntil: "networkidle0" });

	  // Obtain the CDP client and send a custom CDP protocol
	  const client = await page.createCDPSession();
	  try {
		  const res = await client.send('Captchas.automaticSolver',{
			"timeout": 120000,  // Please allow sufficient time, and 2 minutes is recommended.
			"solverType": "cloudflare"   // Use the corresponding type.
		  });
		  
		  // {message: 'Solver meaages', success: true, timestamp: 170000000}
		  console.log("res=", res) 
	  } catch(e) {
		  console.error("err=", e)
	  }
	
	  // Verification
	  await page.screenshot({ path: 'solver_cf.png'});  
	  const title = await page.title();
	  console.log("title=", title);

  } catch (error) {
	  console.error(error);
  }
  finally{
	  console.info("browser closed")
	  await browser.close();
  }
 
}
run();
```

{% endtab %}

{% tab title="Python" %}

```python
import time

from selenium.webdriver.chromium.remote_connection import ChromiumRemoteConnection
from selenium.webdriver import Remote, ChromeOptions

scbr_connection =ChromiumRemoteConnection("https://{AUTH}@hs-browser.thordata.com", 'goog', 'chrome')

with Remote(sbr_connection, options=ChromeOptions()) as driver:
    print('Connected!')

    driver.get('https://example.com')
	
    # Send a custom CDP
    res = driver.execute_cdp_cmd("Captchas.automaticSolver", {
        "timeout": 120000,
        "solverType": "datadome"
    })
    #  {message: 'Solver message', success: true, timestamp: 170000000}
    print(f"Captchas solver result: {res}")
    print("title", driver.title)  
	
    # screenshot
    print('Page screenshot')
    driver.get_screenshot_as_file('./solver_cf.png')
```

{% endtab %}
{% endtabs %}

### **参数规范**

\
发送CDP命令时，必须包含 `solverType` 参数。

```
cloudflare    
google-v2    
datadome
```

### **统一返回结构**

\
无论您使用我们的验证码解决方案破解何种类型的验证码，其返回结构均保持一致。

```
res= {
  message: 'captcha solver success',   //Description
  success: true,  //Sovler status   
  timestamp: 1755763768   //Timestamp
}
```
