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

Python Selenium定位网页下载按钮超时问题求助

问题:Selenium无法定位并点击目标按钮

我正在基于Selenium开发Python脚本,操作指定网页以找到并点击“Download reports for all subtypes”按钮。此前在其他网站上已成功实现该操作,但在此网站运行脚本时触发TimeoutException错误。

运行的Python脚本

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

webdriver_path = r'D:\MyWork\driver\chromedriver.exe'

service = Service(webdriver_path)
chrome_options = Options()

url = "https://www.heatpumpkeymark.com/en/?type=109126&tx_pskeymark_frontend%5Bholder%5D=2482&tx_pskeymark_frontend%5Baction%5D=certificateHolder&tx_pskeymark_frontend%5Bcontroller%5D=Frontend&cHash=1f82e53f3d95d0b9ebfe0c7bb0cdfeb2"
driver = webdriver.Chrome(service=service, options=chrome_options)
driver.get(url)
time.sleep(3)
wait = WebDriverWait(driver, 5)  

class_name = 'btn btn-primary'
text = 'Download reports for all subtypes'

try:
    # button = wait.until(EC.element_to_be_clickable((By.XPATH, f'//button[class()="{class_name}"]')))
    button = wait.until(EC.element_to_be_clickable((By.XPATH, f'//button[text()="{text}"]')))
    
    if button:
        print('Button found')
    else:
        print('Button not found')

finally:
    driver.quit()

报错信息

TimeoutException                          Traceback (most recent call last)
Cell In[9], line 29
     25 text = 'Download reports for all subtypes'
     27 try:
     28     # button = wait.until(EC.element_to_be_clickable((By.XPATH, f'//button[class()="{class_name}"]')))
---> 29     button = wait.until(EC.element_to_be_clickable((By.XPATH, f'//button[text()="{text}"]')))
     31     if button:
     32         print('Button found')

File c:\Users\ctran\AppData\Local\Programs\Python\Python312\Lib\site-packages\selenium\webdriver\support\wait.py:105, in WebDriverWait.until(self, method, message)
    103     if time.monotonic() > end_time:
    104         break
--> 105 raise TimeoutException(message, screen, stacktrace)

TimeoutException: Message: 
Stacktrace:
    GetHandleVerifier [0x00007FF6268F22C2+60002]
    (No symbol) [0x00007FF62686CA59]
    (No symbol) [0x00007FF626727EDA]
    (No symbol) [0x00007FF6267776E6]
    (No symbol) [0x00007FF6267777AC]
...

解决思路

  • 修正元素定位表达式
    原XPATH的text()匹配可能因文本前后存在空格、换行或元素标签不符(比如实际是<a>而非<button>)失效。可改用模糊匹配:

    button = wait.until(EC.element_to_be_clickable((By.XPATH, '//*[contains(text(), "Download reports for all subtypes")]')))
    

    若用class定位,需修正XPATH写法(避免多class的部分匹配问题):

    button = wait.until(EC.element_to_be_clickable((By.XPATH, '//*[contains(concat(" ", @class, " "), " btn btn-primary ")]')))
    
  • 检查并切换iframe
    若按钮嵌套在iframe内,需先切换到对应iframe再定位:

    # 替换为实际iframe的定位方式,比如ID、name
    wait.until(EC.frame_to_be_available_and_switch_to_it((By.ID, "target-iframe-id")))
    # 之后再执行按钮定位逻辑
    
  • 延长等待时长
    页面加载速度可能慢于当前5秒的等待时间,可调整等待时长至10-15秒:

    wait = WebDriverWait(driver, 15)
    
  • 滚动到元素可见区域
    按钮可能位于页面下方未加载的区域,需先滚动到元素位置:

    button = wait.until(EC.presence_of_element_located((By.XPATH, '//*[contains(text(), "Download reports for all subtypes")]')))
    driver.execute_script("arguments[0].scrollIntoView();", button)
    wait.until(EC.element_to_be_clickable((By.XPATH, '//*[contains(text(), "Download reports for all subtypes")]'))).click()
    
  • 规避反爬检测
    网站可能识别自动化工具,可添加配置隐藏Selenium特征:

    chrome_options.add_argument("--disable-blink-features=AutomationControlled")
    chrome_options.add_experimental_option("excludeSwitches", ["enable-automation"])
    chrome_options.add_experimental_option('useAutomationExtension', False)
    

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.23 17:48:09