You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Selenium C#:SendKeys与手动输入的被测页面行为差异问题

Bypassing Anti-Bot Detection for Selenium Input on Registration Pages

Hey Chris, sounds like you're hitting that common roadblock with anti-bot detection—totally frustrating when your automation behaves differently than manual actions! The issue is likely that sites can pick up on the "mechanical" nature of SendKeys (even when looping through characters) compared to how a real human types. Let’s walk through some alternative methods to mimic more natural input and avoid being flagged:

1. Direct DOM Manipulation with JavaScript

This method skips Selenium’s native input entirely by modifying the input field’s value directly via JavaScript. Some sites don’t flag this since it’s how the browser natively handles form data, and you can even trigger required events to make it feel authentic.

// Cast driver to JavaScript executor
IJavaScriptExecutor js = (IJavaScriptExecutor)driver;
string inputValue = "your_new_username";

// Set the input field's value
js.ExecuteScript("arguments[0].value = arguments[1];", target, inputValue);

// Optional: Trigger the 'input' event (some sites rely on this for validation)
js.ExecuteScript("arguments[0].dispatchEvent(new Event('input'));", target);

2. Simulate Human Typing with Actions Class + Random Delays

Your original loop is close, but adding random delays between keystrokes and using Selenium’s Actions class can make the input feel far more natural. Humans don’t type at a perfect uniform speed, so this small change can throw off detection scripts.

Actions actions = new Actions(driver);
string inputText = "your_new_username";
Random random = new Random();

foreach (char c in inputText)
{
    // Add a random delay between 100-300ms (adjust as needed)
    Thread.Sleep(random.Next(100, 300));
    // Use Actions to send each character
    actions.SendKeys(target, c.ToString()).Perform();
}

// Bonus: Throw in a backspace and retype a character occasionally to mimic human errors
Thread.Sleep(random.Next(200, 400));
actions.SendKeys(target, Keys.Backspace).Perform();
Thread.Sleep(random.Next(100, 200));
actions.SendKeys(target, inputText[inputText.Length - 1].ToString()).Perform();

3. Low-Level Keyboard Events via DevTools Protocol

For sites with stricter detection, using the Chrome DevTools Protocol to simulate raw keyboard events is a powerful option. This mimics exactly how a real browser receives input from the OS, making it extremely hard to distinguish from manual actions.

// Access DevTools session (works with Chrome/Edge)
var devTools = driver as IDevTools;
var session = devTools.CreateDevToolsSession();
session.Network.Enable(new Network.EnableCommandSettings());

string text = "your_new_username";
Random random = new Random();

foreach (char c in text)
{
    // Simulate key down event
    var keyDown = new Input.DispatchKeyEventCommandSettings
    {
        Type = Input.KeyEventType.KeyDown,
        Text = c.ToString(),
        UnmodifiedText = c.ToString()
    };
    session.Input.DispatchKeyEvent(keyDown);

    // Simulate key up event
    var keyUp = new Input.DispatchKeyEventCommandSettings
    {
        Type = Input.KeyEventType.KeyUp,
        Text = c.ToString(),
        UnmodifiedText = c.ToString()
    };
    session.Input.DispatchKeyEvent(keyUp);

    // Add random delay between keystrokes
    Thread.Sleep(random.Next(50, 200));
}

4. Copy-Paste Simulation

Mimicking a human copying and pasting text is another low-effort way to avoid detection. Most sites don’t flag paste actions as suspicious since they’re common user behavior.

// Copy the desired text to the clipboard
Clipboard.SetText("your_new_username");

// Simulate clicking the input field and pasting with Ctrl+V
Actions actions = new Actions(driver);
actions.Click(target)
       .KeyDown(Keys.Control)
       .SendKeys("v")
       .KeyUp(Keys.Control)
       .Perform();

Bonus Tips to Avoid Detection

  • Skip Headless Mode: Many sites flag headless browsers by default. If possible, run your automation in a visible browser window.
  • Match Real User-Agent: Ensure your Selenium driver uses the same User-Agent string as a real browser. You can set this via driver options.
  • Add Mouse Movements: Before clicking the input field, move the mouse to it gradually instead of jumping directly to the element. Use Actions.MoveToElement(target).Perform();

Give these methods a try—start with the JavaScript or Actions approach first since they’re simpler to implement. If those still get flagged, the DevTools method is your best bet for more robust evasion.

内容的提问来源于stack exchange,提问作者Chris

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.26 08:49:41