You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Selenium ChromeDriver访问纽约政府网站页面超时缓冲问题求助

Selenium ChromeDriver访问纽约政府网站超时,疑似被反爬拦截

问题描述

作为Python新手,我编写了Selenium脚本试图自动获取纽约政府网站的数据,以简化每周重复工作。页面能正常打开并输入搜索信息,但点击提交后一直缓冲,最终触发超时错误:

selenium.common.exceptions.TimeoutException: Message: timeout: Timed out receiving message from renderer: 300.000

手动用Chrome操作完全正常,怀疑网站检测到Selenium并拦截了请求。已尝试以下方法但均无效:

  • 安装undetected_chromedriver
  • 设置headless=true
  • 配置page_load_strategy为eager
  • 降级Selenium至4.12.0

环境信息:

  • Mac系统
  • Sublime Text编辑器
  • Chrome版本118.0.5993.70
  • Selenium版本4.12.0

我的代码

import setuptools
import selenium
from selenium import webdriver
import undetected_chromedriver as uc
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.chrome.options import Options
from webdriver_manager.chrome import ChromeDriverManager
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import Select
chromeOptions = webdriver.ChromeOptions()
chromeOptions.add_experimental_option("excludeSwitches", ['enable-automation']);


if __name__ == '__main__':
    driver = uc.Chrome(headless=True,use_subprocess=False)
    options = Options()
    options.add_experimental_option("detach", True)
    options = Options()
    options.page_load_strategy = 'eager'
    driver = webdriver.Chrome(service=Service(ChromeDriverManager().install()), options=options)

    driver.get("https://a810-bisweb.nyc.gov/bisweb/PropertyProfileOverviewServlet?/")
    driver.maximize_window()
    boro2Select = Select(driver.find_element("id", "boro2"))
    boro2Select.select_by_visible_text('Queens')
    search_box = driver.find_element("name", "block").send_keys('2433')
    search_box = driver.find_element("name", "lot").send_keys('27')
    goButton = driver.find_element("name", "go3")
    goButton.click()

解决方案

1. 修复undetected_chromedriver的错误使用

你的代码先初始化了uc.Chrome,但随后用普通webdriver.Chrome覆盖了实例,等于完全没用到undetected_chromedriver的反检测能力。正确用法是用uc配置选项并启动浏览器:

import undetected_chromedriver as uc
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.support.ui import Select
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

if __name__ == '__main__':
    options = Options()
    options.page_load_strategy = 'eager'
    options.add_experimental_option("detach", True)
    # 添加核心反检测参数
    options.add_argument("--disable-blink-features=AutomationControlled")
    options.add_experimental_option('useAutomationExtension', False)
    options.add_experimental_option("excludeSwitches", ["enable-automation"])
    
    # 用undetected_chromedriver启动浏览器
    driver = uc.Chrome(options=options, headless=False, use_subprocess=False)
    driver.get("https://a810-bisweb.nyc.gov/bisweb/PropertyProfileOverviewServlet?/")
    
    # 显式等待元素加载后再操作
    boro2Select = Select(WebDriverWait(driver, 10).until(
        EC.presence_of_element_located(("id", "boro2"))
    ))
    boro2Select.select_by_visible_text('Queens')
    
    WebDriverWait(driver, 10).until(
        EC.visibility_of_element_located(("name", "block"))
    ).send_keys('2433')
    
    WebDriverWait(driver, 10).until(
        EC.visibility_of_element_located(("name", "lot"))
    ).send_keys('27')
    
    goButton = WebDriverWait(driver, 10).until(
        EC.element_to_be_clickable(("name", "go3"))
    )
    goButton.click()

2. 补充更多反检测配置

可以添加以下选项进一步模拟真实浏览器行为:

  • options.add_argument("--start-maximized"):启动时直接最大化窗口
  • options.add_argument("--disable-extensions"):禁用浏览器扩展
  • options.add_argument("--no-sandbox"):关闭沙箱模式(Mac环境下可能需要)
  • options.add_argument("--disable-dev-shm-usage"):避免/dev/shm内存不足导致的加载问题
  • options.add_argument("user-agent=Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/118.0.0.0 Safari/537.36"):设置与你Chrome版本匹配的真实用户代理

3. 强制匹配ChromeDriver版本

虽然WebDriverManager会自动匹配版本,但有时仍可能出现兼容性问题,可以手动指定ChromeDriver版本:

from webdriver_manager.chrome import ChromeDriverManager

driver = uc.Chrome(
    service=Service(ChromeDriverManager(version="118.0.5993.70").install()),
    options=options
)

内容的提问来源于stack exchange,提问作者satiric-goose1563

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.08 12:55:38