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

如何使用Python的Selenium点击美国银行模拟器中的指定按钮?

解决Selenium点击美国银行模拟器按钮无响应的问题

针对你遇到的按钮点击无响应问题,以下是几个可行的排查和解决方向:

1. 用显式等待替代固定睡眠

固定sleep(3)完全依赖页面加载速度,稳定性差。换成显式等待,确保元素真正可交互后再执行点击:

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 import webdriver
from selenium.webdriver.chrome.service import Service
from webdriver_manager.chrome import ChromeDriverManager

driver = webdriver.Chrome(service=Service(ChromeDriverManager().install()))
driver.get('https://message.bankofamerica.com/onlinebanking_demo/OLB_Simulator/')

wait = WebDriverWait(driver, 10)

# 等待初始Sign in按钮可点击并点击
login_button = wait.until(EC.element_to_be_clickable((By.ID, "landing_sign")))
login_button.click()

# 等待并点击"Sign in options"
sign_in_options = wait.until(EC.element_to_be_clickable((By.XPATH, "//button[text()='Sign in options']")))
sign_in_options.click()

# 等待并点击"Sign in: Recognized device"
recognized_device_btn = wait.until(EC.element_to_be_clickable((By.XPATH, "//button[text()='Sign in: Recognized device']")))
recognized_device_btn.click()

2. 检查元素是否处于iframe内

如果目标按钮嵌套在iframe中,必须先切换到对应iframe才能操作:

# 先定位iframe(需根据页面实际结构调整选择器)
iframe = wait.until(EC.presence_of_element_located((By.CSS_SELECTOR, "iframe[src*='OLB_Simulator']")))
driver.switch_to.frame(iframe)

# 之后再执行后续的元素定位和点击操作

3. 模拟真实用户的点击动作

有些前端框架会监听鼠标移动等事件,直接调用click()可能无法触发完整的交互逻辑,可以用ActionChains模拟真实点击:

from selenium.webdriver.common.action_chains import ActionChains

action = ActionChains(driver)
action.move_to_element(login_button).click().perform()

4. 排查页面遮罩层干扰

页面加载时可能存在隐藏的加载遮罩层,即便元素已找到,遮罩层也会阻止点击。可以等待遮罩层消失后再操作:

# 等待加载遮罩层不可见(需替换为页面实际的遮罩层选择器)
wait.until(EC.invisibility_of_element_located((By.CLASS_NAME, "loading-overlay")))

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.31 09:31:04