如何隐藏浏览器处于SeleniumWebdriver控制状态?解决验证码验证差异
Hey, I’ve dealt with this exact problem countless times—those anti-bot systems are sharp at picking up Selenium’s default traces. Here are the most reliable fixes to get back to that simple checkbox captcha:
1. Disable Chrome’s Automation Flag
Chrome’s dead giveaway is the window.navigator.webdriver property and the "Automation Controlled" notification. You can squash both by adding specific launch arguments to your Selenium setup:
from selenium import webdriver from selenium.webdriver.chrome.options import Options chrome_options = Options() # This is the key flag to bypass detection chrome_options.add_argument("--disable-blink-features=AutomationControlled") # Add other flags to mimic a real browser session chrome_options.add_argument("--start-maximized") chrome_options.add_argument("--no-sandbox") chrome_options.add_argument("--disable-dev-shm-usage") # Optional: Disable extensions and pop-ups chrome_options.add_argument("--disable-extensions") chrome_options.add_argument("--disable-popup-blocking") driver = webdriver.Chrome(options=chrome_options)
2. Use Undetected-Chromedriver (The Easiest Fix)
If manual flag tweaks feel too tedious, undetected-chromedriver is a modified Selenium ChromeDriver built specifically to evade anti-bot detection. It handles all the hidden tweaks automatically:
First install it:
pip install undetected-chromedriver
Then use it like this (super similar to standard Selenium, but with better stealth):
import undetected_chromedriver as uc # Initialize the driver with default stealth settings driver = uc.Chrome() driver.get("your-target-url-here")
This library often skips straight to the simple checkbox captcha because it masks all of Selenium’s usual fingerprints.
3. Mimic Real User Behavior
Even with stealth tweaks, robotic-looking actions can trigger stricter captchas. Add these touches to make your script act human:
- Random waits: Instead of fixed
time.sleep(2), usetime.sleep(random.uniform(1.5, 3.5))between actions. - Mouse movements: Use Selenium’s
ActionChainsto move the mouse around randomly before clicking elements. - Scroll first: Scroll up/down the page a bit before interacting with the captcha.
- Avoid instant navigation: Open a blank page first (
driver.get("about:blank")), wait a second, then load your target URL.
4. Tweak Browser Fingerprint (Advanced)
For extra stubborn sites, you can further mask your browser identity:
- Set a realistic user-agent: Grab a current Chrome UA string and add it via
chrome_options.add_argument("user-agent=Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/118.0.0.0 Safari/537.36") - Override the webdriver property: Run this script right after launching the driver to hide the webdriver flag:
driver.execute_script("Object.defineProperty(navigator, 'webdriver', {get: () => undefined})")
Combining these methods should get you past the puzzle captchas and back to the simple checkbox. Start with undetected-chromedriver—it’s the most foolproof option for most cases.
内容的提问来源于stack exchange,提问作者kris82pl

