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

Python/Selenium点击按钮触发TimeoutException问题求助

问题:Selenium脚本点击下载按钮超时

我编写了一个Python/Selenium脚本,需求是自动点击网页按钮下载CSV文件,但执行时一直触发TimeoutException。确认XPath正确,尝试通过元素属性定位也遇到相同的超时问题。

原脚本代码

from selenium import webdriver
from selenium.webdriver.support.ui import Select
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.chrome.service import Service
from webdriver_manager.chrome import ChromeDriverManager
import pandas as pd
import time

custom_options = webdriver.ChromeOptions()
custom_options.add_experimental_option('prefs', {'intl.accept_languages': 'en,en_US'})

service = Service(executable_path=ChromeDriverManager().install())
driver = webdriver.Chrome(service=service, options=custom_options)

url = "https://data.gov.il/dataset/aircraft_data_il/resource/bc00ed41-75d0-4d0f-9eca-3cd0a2c332cc?fbclid=IwAR0ECd4VNpZ8FRLZSswOw-xM2EVpEeosJvxmhrYDt2bwvYSe1myQ9McCQ9g"

custom_options = webdriver.ChromeOptions()
custom_options.add_argument("start-maximized")
custom_options.add_argument("disable-infobars")
custom_options.add_argument("--disable-extensions")
custom_options.add_argument("--disable-gpu")
custom_options.add_argument("--disable-dev-shm-usage")
custom_options.add_argument("--no-sandbox")
custom_options.add_argument("--remote-debugging-port=9222")
custom_options.add_argument("user-agent=Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/100.0.0.0 Safari/537.36")

driver.get(url)

download_button_xpath = "//*[@id='content']/div[3]/section/div/div[1]/ul/li[2]/div/button"

wait = WebDriverWait(driver, 30) # Increase wait time if necessary
download_button = wait.until(EC.presence_of_element_located((By.XPATH, download_button_xpath)))

download_button.click()

time.sleep(5)

driver.quit()

print("CSV file downloaded successfully!")

报错信息

line 40, in <module>
    download_button = wait.until(EC.presence_of_element_located((By.XPATH, download_button_xpath)))
  File "C:\Users\xxxxx\AppData\Local\Programs\Python\Python311\Lib\site-packages\selenium\webdriver\support\wait.py", line 89, in until
    raise TimeoutException(message, screen, stacktrace)
selenium.common.exceptions.TimeoutException: Message: 
Stacktrace:
Backtrace:
    GetHandleVerifier [0x009EDCE3+50899]
    (No symbol) [0x0097E111]
    (No symbol) [0x00885588]
    (No symbol) [0x008B08F9]
    (No symbol) [0x008B0AFB]
    (No symbol) [0x008DF902]
    (No symbol) [0x008CB944]
    (No symbol) [0x008DE01C]
    (No symbol) [0x008CB6F6]
    (No symbol) [0x008A7708]
    (No symbol) [0x008A886D]
    GetHandleVerifier [0x00C53EAE+2566302]
    GetHandleVerifier [0x00C892B1+2784417]
    GetHandleVerifier [0x00C8327C+2759788]
    GetHandleVerifier [0x00A85740+672048]
    (No symbol) [0x00988872]
    (No symbol) [0x009841C8]
    (No symbol) [0x009842AB]
    (No symbol) [0x009771B7]
    BaseThreadInitThunk [0x76146B89+25]
    RtlGetFullPathName_UEx [0x774B8F9F+1215]
    RtlGetFullPathName_UEx [0x774B8F6D+1165]

解决方案

问题根源分析

  1. 重复定义ChromeOptions导致配置失效:原脚本先创建custom_options并传给driver,之后又重新定义了新的配置,但未应用到已启动的driver,导致页面可能因窗口未最大化、UA不正确等问题渲染异常。
  2. 等待条件不合适:presence_of_element_located仅判断元素在DOM中存在,但元素可能还未加载完成或被遮挡,无法点击。
  3. 定位方式过于脆弱:原XPath依赖完整的DOM层级结构,页面布局稍有变化就会失效。

修改后的脚本

from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from selenium.webdriver.chrome.service import Service
from webdriver_manager.chrome import ChromeDriverManager
import time

# 合并所有Chrome配置到同一个Options对象
custom_options = webdriver.ChromeOptions()
custom_options.add_experimental_option('prefs', {'intl.accept_languages': 'en,en_US'})
custom_options.add_argument("start-maximized")
custom_options.add_argument("disable-infobars")
custom_options.add_argument("--disable-extensions")
custom_options.add_argument("--disable-gpu")
custom_options.add_argument("--disable-dev-shm-usage")
custom_options.add_argument("--no-sandbox")
custom_options.add_argument("user-agent=Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/100.0.0.0 Safari/537.36")

service = Service(executable_path=ChromeDriverManager().install())
driver = webdriver.Chrome(service=service, options=custom_options)

url = "https://data.gov.il/dataset/aircraft_data_il/resource/bc00ed41-75d0-4d0f-9eca-3cd0a2c332cc"
driver.get(url)

wait = WebDriverWait(driver, 30)

# 先处理可能的cookie弹窗(如果页面有)
try:
    cookie_accept = wait.until(EC.element_to_be_clickable((By.XPATH, "//button[contains(text(), 'Accept')]")))
    cookie_accept.click()
except:
    pass  # 无弹窗则跳过

# 使用更稳定的定位方式:根据按钮的data-format属性和文本
download_button = wait.until(EC.element_to_be_clickable((By.XPATH, "//button[contains(@data-format, 'csv') and contains(text(), 'CSV')]")))

download_button.click()

time.sleep(5)

driver.quit()

print("CSV文件下载成功!")

关键优化点

  • 合并所有Chrome配置到一个ChromeOptions对象,确保所有设置生效。
  • 使用element_to_be_clickable等待条件,确保元素可交互后再点击。
  • 更换为基于元素属性(data-format)和文本的XPath,提升定位稳定性。
  • 添加cookie弹窗处理逻辑,避免弹窗遮挡下载按钮。

内容的提问来源于stack exchange,提问作者Mark k

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.24 07:07:05