Anti-Detection Improvements for Browser Automation #55
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Overview
This PR implements comprehensive anti-detection improvements to make kdriver-based browser automation indistinguishable from human interaction. These changes address detection by anti-bot systems by ensuring all user interactions generate
isTrusted: trueevents and exhibit natural human-like behavior patterns.Problem Statement
Current kdriver automation is detectable because:
mouseClick()fails for off-viewport elements, forcing fallback to JavaScriptelement.click()which generatesisTrusted: falseeventsChanges Implemented
1. 🔴 P0 - CRITICAL: Fixed mouseClick() for Off-Viewport Elements
Before: Method abandoned silently if element was outside viewport, requiring JavaScript click fallback.
After:
scrollIntoView()to bring element into viewport before interactionrequestAnimationFramewith promise to ensure scroll completesInput.dispatchMouseEvent→isTrusted: true✅Impact: Eliminates the root cause of detection - all click events are now trusted.
2. 🟡 P1 - HIGH: Added Coordinate Jitter
Before: Clicks always at exact center:
(rect.left + rect.width / 2, rect.top + rect.height / 2)After: Random offset applied to both mouseMove and mouseClick:
Impact: Mimics natural human behavior - humans never click the exact geometric center.
3. 🟡 P1 - HIGH: Randomized Timing Delays
Before:
tab.sleep(10)- constant 10ms delaytab.sleep(50)- constant 50ms between press/releaseAfter:
Impact: Removes constant temporal patterns that can be detected statistically.
4. 🟢 P2 - MEDIUM: Natural Mouse Trajectory (Bezier Curves)
Before: Mouse teleported instantly to target coordinates.
After: Implemented
mouseMoveWithTrajectory()using quadratic Bezier curves:B(t) = (1-t)²P₀ + 2(1-t)tP₁ + t²P₂Impact: Mouse movement indistinguishable from human cursor paths.
5. 🔵 P3 - LOW: Fixed clearInputByDeleting() to Use CDP
Before: Used JavaScript
KeyboardEventdispatch →isTrusted: falseAfter: Uses CDP
Input.dispatchKeyEventwith proper keyDown/keyUp sequenceImpact: Keyboard events now generate
isTrusted: true.Testing
./gradlew :core:jvmTestTechnical Details
New Components
lastMouseX,lastMouseY) for trajectory continuitykotlin.random.Randomfor all randomizationFiles Modified
core/src/commonMain/kotlin/dev/kdriver/core/dom/DefaultElement.ktBenefits
✅ Trusted Events: All interactions generate
isTrusted: truevia CDP protocol✅ Human-like Behavior: Random jitter, variable timing, curved trajectories
✅ No Detection Patterns: Eliminates constant coordinates, fixed delays, teleportation
✅ Backwards Compatible: All existing tests pass, no breaking API changes
✅ Production Ready: Tested and ready for immediate use
Usage Example
References
This implementation follows anti-detection best practices for browser automation and specifically addresses detection patterns used by systems like DataDome, PerimeterX, and similar anti-bot solutions.
🤖 Generated with Claude Code