使用Selenium爬取页面时遇验证码需终止程序,求实现方法
Great question—when scraping at scale, accidentally dismissing captcha prompts is a surefire way to get your IP or account blocked. Let’s walk through a reliable solution to detect these captcha alerts and terminate your program before that happens.
Step 1: Differentiate Captcha Alerts from Regular Alerts
Most regular alerts have generic messages (like "Are you sure?" or "Action completed!"), while captcha prompts will include keywords tied to security checks. We can leverage this to build a conditional check before automatically accepting alerts.
Here’s a practical code snippet:
from selenium.common.exceptions import NoAlertPresentException import sys def handle_alert(browser): try: # Switch to the alert if one exists alert = browser.switch_to.alert alert_text = alert.text.lower() # Define keywords that signal a captcha prompt (adjust these for your target site) captcha_triggers = ["captcha", "security check", "人机验证", "verify you're human"] if any(trigger in alert_text for trigger in captcha_triggers): print("⚠️ Captcha detected! Terminating program to avoid being blocked.") # Dismiss the alert (cleaning up before exit is better than leaving it hanging) alert.dismiss() browser.quit() sys.exit(1) else: # It's a regular alert—safe to accept alert.accept() except NoAlertPresentException: # No alert to handle, proceed with your scraping logic pass # Call this function after navigating to each page handle_alert(browser)
Step 2: Add Robust Waiting with WebDriverWait
Instead of immediately checking for alerts (which can miss slow-loading prompts), use WebDriverWait to wait for alerts to appear with a timeout. This makes your code far more reliable:
from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.support import expected_conditions as EC from selenium.common.exceptions import TimeoutException import sys def handle_alert_safely(browser): try: # Wait up to 5 seconds for an alert to show up alert = WebDriverWait(browser, 5).until(EC.alert_is_present()) alert_text = alert.text.lower() captcha_triggers = ["captcha", "security check", "人机验证"] if any(trigger in alert_text for trigger in captcha_triggers): print("⚠️ Captcha encountered! Stopping scraping immediately.") alert.dismiss() browser.quit() sys.exit(1) else: alert.accept() except TimeoutException: # No alert appeared in time—continue scraping pass
Step 3: Check for Non-Alert Captcha Elements
Some sites don’t use alerts for captchas—they show inline modals or form elements. Add a check for these cases too:
from selenium.common.exceptions import NoSuchElementException def check_for_captcha(browser): try: # Adjust the selector to match your target site's captcha container (use ID, class, or XPath) captcha_box = browser.find_element(By.CLASS_NAME, "captcha-modal") if captcha_box.is_displayed(): print("⚠️ Captcha found on page! Terminating program.") browser.quit() sys.exit(1) except NoSuchElementException: # No captcha element found—keep going pass # Call this after page loads, alongside the alert handler check_for_captcha(browser)
Bonus Tips to Reduce Captcha Triggers
- Add random delays between requests (e.g.,
time.sleep(random.uniform(1, 3))) to mimic human browsing speed. - Rotate user agents and proxies if you’re scraping very large volumes of pages.
- Avoid rapid-fire clicks or form submissions—pace your actions like a real user would.
内容的提问来源于stack exchange,提问作者Luiza Rodrigues

