Selenium Stealth
Selenium Stealth is a Python library designed to make Selenium WebDriver more stealthy, aiming to bypass anti-bot detection systems on websites. It achieves this by modifying various browser properties and behaviors that typically reveal automated browsing, such as the `navigator.webdriver` flag, user-agent, and WebGL fingerprints. The library currently supports Chrome/Chromium and is at version 1.0.6. While still functional for many use cases, its last update on PyPI was in November 2020, and there is an actively maintained fork, `stealthenium`, for those seeking ongoing development.
Common errors
-
selenium.common.exceptions.SessionNotCreatedException: Message: session not created: DevToolsActivePort file doesn't exist
cause This typically indicates an incompatibility between your Chrome browser version and the ChromeDriver executable version, or issues with Chrome launching (e.g., insufficient memory, corrupted user profile, or sandbox issues in Docker).fixUpdate your ChromeDriver to match your Chrome browser version. Check `chrome://settings/help` for your Chrome version, then download the corresponding ChromeDriver from the official site. Also, ensure sufficient system resources and add `--no-sandbox` if running in a containerized environment. -
Websites are still detecting my Selenium script as a bot, despite using selenium-stealth.
cause While `selenium-stealth` addresses many common detection vectors (like `navigator.webdriver`), advanced anti-bot systems employ more sophisticated techniques, including analyzing behavior patterns (speed, mouse movements), IP reputation, CAPTCHA challenges, and complex browser fingerprinting beyond what `selenium-stealth` alone can spoof.fixBeyond `selenium-stealth`, consider: 1) Using `undetected_chromedriver` (a separate project designed for this purpose). 2) Adding random delays (`time.sleep()`) and simulating human-like interactions (scrolls, mouse movements). 3) Rotating IP addresses with proxies. 4) Avoiding headless mode or configuring it very carefully. 5) Maintaining browser profiles (cookies) to build 'trust'. -
I get inconsistent results or elements not found when rapidly interacting with a page after applying stealth.
cause Even with stealth, rapid, robotic interactions or failure to wait for dynamic content to load can trigger anti-bot measures or simply cause race conditions in your script. Websites expect human-like interaction speeds.fixImplement `time.sleep()` for random delays between actions. Use Selenium's explicit waits (`WebDriverWait` and `expected_conditions`) to ensure elements are present, visible, and clickable before interacting with them. Mimic human scrolling behavior.
Warnings
- deprecated The `selenium-stealth` project has not received updates on PyPI since November 2020 and its maintenance status is considered inactive. A more actively maintained fork, `stealthenium`, is available and explicitly states it's a fork of this unmaintained project. Consider migrating to `stealthenium` for ongoing support and updates.
- gotcha Selenium Stealth currently only supports Chrome/Chromium browsers. It does not provide stealth capabilities for Firefox, Edge, or other browsers.
- gotcha While `selenium-stealth` enhances anonymity, running browsers in headless mode (`--headless` Chrome option) can still be a strong indicator of automation and may lead to detection on sophisticated anti-bot sites.
- gotcha Attempting to manually spoof browser properties like `User-Agent` via Chrome options without ensuring consistency with other `navigator` properties (e.g., `platform`) can lead to immediate bot detection. `selenium-stealth` handles many of these internally.
Install
-
pip install selenium-stealth
Imports
- stealth
from selenium_stealth import stealth
Quickstart
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.chrome.options import Options
from selenium_stealth import stealth
import time
# Set up Chrome options
chrome_options = Options()
chrome_options.add_argument("start-maximized")
chrome_options.add_experimental_option("excludeSwitches", ["enable-automation"])
chrome_options.add_experimental_option('useAutomationExtension', False)
# Configure ChromeDriver service (optional for Selenium 4.6+ if Chrome is in PATH)
# If ChromeDriver is not in PATH, uncomment and set executable_path
# service = Service(executable_path='/path/to/chromedriver')
# driver = webdriver.Chrome(service=service, options=chrome_options)
# For Selenium 4.6+ and Chrome in PATH, direct initialization is often sufficient
driver = webdriver.Chrome(options=chrome_options)
# Apply stealth settings
stealth(
driver,
languages=["en-US", "en"],
vendor="Google Inc.",
platform="Win32",
webgl_vendor="Intel Inc.",
renderer="Intel Iris OpenGL Engine",
fix_hairline=True,
)
print("Navigating to a test site...")
driver.get("https://bot.sannysoft.com/") # A common site to test bot detection
time.sleep(5) # Give some time for the page to load and scripts to run
print(f"Page title: {driver.title}")
# Add your scraping logic here
driver.quit()
print("Browser closed.")