Solving reCAPTCHA v2 with PuppeteerSharp and DeathByCaptcha in C
Puppeteer is a Node.js library, so C# teams use PuppeteerSharp, a community port that tracks the same API. The captcha problem does not change: the browser can fill the form, but the token has to come from somewhere.
This example opens Google's reCAPTCHA v2 demo with PuppeteerSharp, reads the sitekey, requests a token from DeathByCaptcha, injects it and submits the form. The token is never printed.
The example lives in the official .NET client:
DBC_Examples/PuppeteerRecaptchaV2Example.cs
What you need
- .NET SDK that supports
net8.0ornet10.0. PuppeteerSharp does not ship anet6.0build, so the sample and its test are excluded from that target. - Chrome. The sample uses
CHROME_BINwhen it points to an existing browser and otherwise downloads Chrome for Testing through PuppeteerSharp. - A DeathByCaptcha account with balance.
Credentials
Set the variables in the environment or in a .env file at the repository
root:
DBC_USERNAME=your_username
DBC_PASSWORD=your_password
The sample never prints the token and never enables verbose output.
Run it
dotnet run --project DBC_Examples/DBC_Examples.csproj -c Release -f net10.0 \
-p:ExamplesStartupObject=DeathByCaptcha.PuppeteerRecaptchaV2Example
--headed shows the browser; headless is the default. A URL argument
replaces the demo page.
The flow
Launch the browser, resolve Chrome and open the demo:
string chromePath = await ResolveChromePathAsync();
await using var browser = await Puppeteer.LaunchAsync(new LaunchOptions
{
Headless = headless,
ExecutablePath = chromePath,
Args = new[] { "--no-sandbox", "--disable-dev-shm-usage" },
});
await using var page = await browser.NewPageAsync();
await page.GoToAsync(pageUrl, new NavigationOptions { Timeout = 60000 });
await page.WaitForSelectorAsync("#recaptcha-demo",
new WaitForSelectorOptions { Timeout = 20000 });
Read the sitekey and request a type 4 token:
string sitekey = await page.EvaluateFunctionAsync<string>(
"() => document.querySelector('#recaptcha-demo').getAttribute('data-sitekey')");
string tokenParams = JsonSerializer.Serialize(new Dictionary<string, string>
{
["googlekey"] = sitekey,
["pageurl"] = pageUrl,
});
var captchaData = new Hashtable
{
["type"] = 4,
["token_params"] = tokenParams,
};
Client client = new HttpClient(username, password);
Captcha solution = client.Decode(Client.DefaultTokenTimeout, captchaData);
Inject the token as a function argument, fire the events and call the widget callback when the page exposes it:
await page.EvaluateFunctionAsync<bool>(
@"(token) => {
const textarea = document.getElementsByName('g-recaptcha-response')[0]
|| document.getElementById('g-recaptcha-response');
if (!textarea) return false;
textarea.style.display = 'block';
textarea.value = token;
textarea.dispatchEvent(new Event('input', { bubbles: true }));
textarea.dispatchEvent(new Event('change', { bubbles: true }));
const clients = window.___grecaptcha_cfg && window.___grecaptcha_cfg.clients;
// ... call the first callback found ...
return true;
}",
solution.Text);
Then submit and wait for the success selector.
Why the callback matters
Filling the hidden field is not always enough. Many forms keep the submit
button disabled until the widget callback runs. The sample walks
___grecaptcha_cfg.clients and calls the first callback it finds, which is
what the browser would have done after a human solved the challenge.
The sample also checks that the token was actually written to the field. A form submitted with an empty token is a silent failure; the example prefers to stop and say so.
Things to know
- PuppeteerSharp is a port, not Puppeteer. It is actively maintained and tracks the upstream API, but it is not published by the Puppeteer team. Name it correctly in your own documentation.
- The sample uses Google's demo page. It is the vendor's own test page. Do not point it at a site you do not own or have permission to test.
- IP-bound types need a proxy. Turnstile, WAF and DataDome tokens are tied to the exit IP; the browser and the service must share it.
- Verbose is off by default. The client used to print every request and response, including the solved token. Now it only does so when the caller enables it.
- CI runs the same flow. The repository runs the sample as an xUnit test on every push, with the account secrets in GitHub.
Next steps
The same call shape works for the other token types: change the type and
the parameter key (token_params, turnstile_params, waf_params). The
repository has per-type examples and a Playwright sample with the same
injection pattern.
- .NET client: https://github.com/deathbycaptcha/deathbycaptcha-api-client-dotnet
- API documentation: https://deathbycaptcha.com/api

English
Spanish
Russian
Chinese
French
Hindi
Arabic
Bengali
Indonesian
Portuguese
com, 