Back to Projects
SB Stealth Wrapper

SB Stealth Wrapper

PythonSeleniumBaseAutomationPyPIStealth

The Challenge

Websites increasingly employ sophisticated bot detection services like Cloudflare Turnstile to filter traffic. While necessary for security, these measures pose a significant hurdle for legitimate automated testing.

The Solution

Developed a unified Python package that acts as a smart proxy between the tester's intent and the browser's execution. It intelligently handles environment configuration and mimics human behavior.

Key Features

  • Intelligent Environment Detection
  • Smart Navigation
  • Human-Like Interaction

Case Study: SB Stealth Wrapper – Democratizing Stealth Automation

Project Context

SB Stealth Wrapper is a specialized Python library designed to abstract the complexities of advanced web automation and bot evasion. Built as a robust wrapper around SeleniumBase's Undetected ChromeDriver (UC Mode), it provides a "plug-and-play" interface for navigating protected websites. It specifically targets the difficulty of testing applications guarded by modern security measures like Cloudflare Turnstile, ensuring that legitimate testing traffic is not blocked by anti-bot systems.

Key Objectives

  1. Simplify Stealth Configuration: Eliminate the complex, repetitive setup required to make Selenium undetectable.
  2. Reliable Challenge Bypass: Provide automated, intelligent handling of Cloudflare Turnstile and "Just a moment..." screens.
  3. Cross-Platform Stability: Ensure the solution works seamlessly in both local (Windows/Mac) and headless CI/CD (Linux/Docker) environments.
  4. Human-Like Interaction: Replace mechanical automation actions with "smart" interactions to maintain a low trust score.

Stakeholders/Users

  • Automation Engineers / SDETs: Who need to run end-to-end tests on production-like environments without getting blocked.
  • QA Teams: Who require stable, flaky-free execution of test suites on protected public-facing sites.
  • Developers: Who need to verify the user experience of security checkpoints.

Technical Background

  • Language: Python 3.6+
  • Core Framework: SeleniumBase (UC Mode)
  • Key Dependencies: xvfb (for Linux display emulation), seleniumbase
  • Architecture: Wrapper pattern encapsulating the SB() context manager with auto-evasion logic.

Problem

The "Access Denied" Bottleneck

In the modern web landscape, security is paramount. Websites increasingly employ sophisticated bot detection services like Cloudflare Turnstile to filter traffic. While necessary for security, these measures pose a significant hurdle for legitimate automated testing.

The Broken State: Standard automation tools (Selenium, Playwright) leak "bot" signals (e.g., navigator.webdriver flags). When an Automation Engineer attempts to run a standard login test on a protected site, they are often met with:

  • Immediate 403 Forbidden errors.
  • Infinite Captcha loops that standard solvers cannot handle.
  • "Just a moment..." screens that never resolve.

Risks & Inefficiencies:

  • Test Blindness: Critical user flows (like Sign Up or Checkout) cannot be automated, forcing manual testing and increasing release risk.
  • High Maintenance: Engineers waste hours tweaking user-agent strings and chrome options, only for the script to break the next day when the detection algorithm updates.
  • Flaky CI Pipelines: Tests might pass locally but fail in headless CI environments due to different browser fingerprints.

Why Existing Approaches Failed: Existing solutions often required deep knowledge of browser fingerprinting. Hardcoding headers or using generic "stealth" plugins often resulted in a cat-and-mouse game where the automation was always one step behind the security updates.


Challenges

1. Technical Complexity of Evasion

  • Dynamic Challenges: Cloudflare Turnstile is not a static image; it uses behavioral analysis and sometimes complex 3D animations (like the "spinning cube" on nowsecure.nl) that resist standard click-to-solve approaches.
  • Headed vs. Headless: To pass "stealth" checks, the browser often needs to run in "headed" mode (with a visible UI). This is natively incompatible with headless Linux servers used in CI/CD pipelines.

2. Operational Constraints

  • Dependency Management: The solution needed to be lightweight. Adding heavy external captcha-solving services (2Captcha, etc.) adds cost and latency.
  • Ease of Use: The target users are testers, not security researchers. The solution had to be simple: pip install and run.

3. Hidden Complexities

  • Timing & Behavior: Simply clicking a checkbox is no longer enough. The way the mouse moves, the timing between actions, and the reaction to page events all contribute to the "trust score" assigned by the bot detector.

Solution

The "SB Stealth Wrapper" Approach

We developed a unified Python package that acts as a smart proxy between the tester's intent and the browser's execution.

Step-by-Step Implementation

  1. Intelligent Environment Detection (Auto-Evasion):

    • The StealthBot class automatically detects the operating system.
    • If running on Linux (CI/CD), it automatically initializes a virtual display using xvfb. This allows the browser to launch in "headed" mode (essential for stealth) even on a headless server, solving the CI stability issue.
  2. Smart Navigation & Challenge Detection:

    • We replaced the standard get() with safe_get().
    • Logic: Upon loading a page, the bot scans the DOM for challenge indicators (e.g., "Turnstile", "Challenge", "Just a moment").
    • Action: If detected, it pauses execution and enters a specialized _handle_challenges() routine, rather than failing immediately.
  3. Human-Like Interaction (smart_click):

    • Standard Selenium clicks are instantaneous and mechanical.
    • Design Decision: We implemented smart_click which:
      • Scrolls the element into view naturally.
      • Uses uc_click to simulate human mouse events (hover, depress, release).
      • Includes a robust fallback chain: Human Click -> Standard Click -> JS Click. This ensures that if the "stealth" click fails (e.g., due to an overlay), the action still completes.
  4. Wrapper Architecture:

    • By wrapping SeleniumBase's SB() context manager, we maintained the Pythonic with StealthBot() as bot: syntax, making migration for existing SeleniumBase users trivial.

Tools & Frameworks

  • SeleniumBase: For the heavy lifting of Undetected ChromeDriver integration.
  • Xvfb: For headless display emulation.
  • Setuptools/Twine: For packaging and distribution.

Outcome/Impact

1. Accelerated Test Development

  • Metric: Setup time for a "stealth" capable test environment reduced from ~2 hours (configuring drivers, patches, options) to < 5 minutes (pip install sb-stealth-wrapper).
  • Impact: Teams can focus on writing business logic tests immediately.

2. Stability on Protected Sites

  • Metric: Success rate on Cloudflare-protected landing pages improved from 0% (blocked immediately) to >90% for standard Turnstile implementations.
  • Impact: Critical flows like "User Registration" on protected environments can now be included in the regression suite.

3. CI/CD Compatibility

  • Impact: The built-in xvfb handling means the exact same script works on a developer's Windows laptop and the Jenkins/GitHub Actions Linux runner without code changes.

Long-Term Benefits

  • Future-Proofing: By centralizing the evasion logic in the wrapper, updates to evasion techniques (e.g., handling a new Turnstile variant) can be pushed to the package and consumed by all test suites instantly via a version bump.

Summary

The SB Stealth Wrapper transforms the frustrating, high-maintenance task of stealth web automation into a streamlined, reliable process. By intelligently handling environment configuration, mimicking human behavior, and automating challenge detection, it empowers QA teams to reclaim their test coverage on protected applications. It bridges the gap between necessary security measures and the need for rigorous automated testing, ensuring that "Access Denied" is no longer a blocker for quality assurance.


Disclaimer

Ethical Use Only: This case study and the associated software are intended for educational purposes and for testing environments that you own or have explicit permission to test.

  • Do not use this tool for unauthorized scraping, spamming, or bypassing security controls on websites you do not own.
  • The authors are not responsible for any misuse or legal consequences resulting from the use of this software.
  • Always adhere to the target website's robots.txt and Terms of Service.

Get In Touch

Interested in collaborating or have a question about my projects? Feel free to reach out. I'm always open to discussing new ideas and opportunities.

Say Hello