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

Python Selenium无法定位Subscan下载按钮Xpath及点击被拦截问题

解决方案

方法1:先处理遮挡的Banner

页面顶部的Banner会拦截点击操作,先将其关闭或隐藏后再操作目标按钮:

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

# 初始化Chrome驱动
s = Service('CHROMEDRIVER LOCATION')
op = webdriver.ChromeOptions()
driver = webdriver.Chrome(service=s, options=op)

link = "https://polkadot.subscan.io/account?role=all&page=1"
driver.get(link)

wait = WebDriverWait(driver, 10)

# 优先尝试点击Banner的关闭按钮
try:
    close_btn = wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, ".banner .close")))
    close_btn.click()
except:
    # 若找不到关闭按钮,用JS强制隐藏Banner
    driver.execute_script("document.querySelector('.banner').style.display = 'none';")

# 等待下载按钮可点击后执行点击
download_btn = wait.until(EC.element_to_be_clickable((By.XPATH, "//*[@id='app']/main/div/div/div[5]/div/div[3]/div[1]/div/div")))
download_btn.click()

方法2:用JavaScript直接触发点击

JS点击不受页面元素遮挡影响,可直接触发目标按钮的点击事件:

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

# 初始化Chrome驱动
s = Service('CHROMEDRIVER LOCATION')
op = webdriver.ChromeOptions()
driver = webdriver.Chrome(service=s, options=op)

link = "https://polkadot.subscan.io/account?role=all&page=1"
driver.get(link)

wait = WebDriverWait(driver, 10)

# 等待下载按钮加载完成
download_btn = wait.until(EC.presence_of_element_located((By.XPATH, "//*[@id='app']/main/div/div/div[5]/div/div[3]/div[1]/div/div")))

# 通过JS直接点击元素
driver.execute_script("arguments[0].click();", download_btn)

关键说明

  • 使用WebDriverWait等待元素加载,避免因页面未完全渲染导致的元素定位失败
  • 方法1更贴近用户真实操作流程,方法2是遮挡类问题的通用解决方案

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.07 18:31:46