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

使用Python Selenium点击Bollinger Band复选框截图股票图表遇交互错误求助

问题解决:Selenium操作Chartink图表勾选布林带失败修复

问题说明

需要用Python的Selenium工具对股票价格图表页面生成截图,截图前需通过「展开Indicators -> Upper Overlays -> Bollinger Band」路径勾选布林带复选框修改图表默认设置,但当前代码抛出ElementNotInteractableException错误。

错误信息

ElementNotInteractableException           Traceback (most recent call last)
Cell In[42], line 37
     34 bollinger_band_checkbox = upper_overlays_form.find_element(By.ID, "BBm")
     36 # Click the "Bollinger Band" checkbox
---> 37 bollinger_band_checkbox.click()

ElementNotInteractableException: Message: element not interactable
  (Session info: headless chrome=119.0.6045.160)

原代码

from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.by import By
import os
import pandas as pd

# Replace this with the actual path to chromedriver.exe on your machine
chrome_driver_path = r'C:\Users\Administrator\chromedriver-win64\chromedriver.exe'

# Folder to save screenshots
screenshot_folder = r'G:\My Drive\WeeklyCharts_RSI_Midcap'

# Set up Chrome options for a headless browser
chrome_options = Options()
chrome_options.add_argument('--headless')  # Run Chrome in headless mode (no GUI)

# Create a new Chrome driver with options and specified executable path
chrome_service = Service(chrome_driver_path)
driver = webdriver.Chrome(service=chrome_service, options=chrome_options)

# Set the window size
driver.set_window_size(1980, 1080)

url = r'https://chartink.com/stocks/iex.html'

# Navigate to the URL
driver.get(url)
    
# Locate the "Upper Overlays" form by ID
upper_overlays_form = driver.find_element(By.ID, "newone2")

# Locate the "Bollinger Band" checkbox within the form
bollinger_band_checkbox = upper_overlays_form.find_element(By.ID, "BBm")

# Click the "Bollinger Band" checkbox
bollinger_band_checkbox.click()

# Capture screenshot
screenshot_name = 'IEX.png'
screenshot_path = os.path.join(screenshot_folder, screenshot_name)

# Take the screenshot
driver.save_screenshot(screenshot_path)

# Close the Chrome driver
driver.quit()

修正后的代码及说明

错误原因:原代码未先展开「Indicators」和「Upper Overlays」菜单,导致复选框处于隐藏状态无法交互;同时未等待元素完全加载,也可能引发该错误。

修正后的代码:

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

# ChromeDriver路径
chrome_driver_path = r'C:\Users\Administrator\chromedriver-win64\chromedriver.exe'
# 截图保存目录
screenshot_folder = r'G:\My Drive\WeeklyCharts_RSI_Midcap'

# 配置Chrome选项
chrome_options = Options()
chrome_options.add_argument('--headless=new')  # 使用新版headless模式,兼容性更好
chrome_options.add_argument('--window-size=1980,1080')
chrome_options.add_argument('--disable-gpu')
chrome_options.add_argument('--no-sandbox')

# 初始化驱动
chrome_service = Service(chrome_driver_path)
driver = webdriver.Chrome(service=chrome_service, options=chrome_options)

try:
    url = 'https://chartink.com/stocks/iex.html'
    driver.get(url)
    wait = WebDriverWait(driver, 10)  # 10秒显式等待

    # 1. 展开Indicators菜单
    indicators_menu = wait.until(EC.element_to_be_clickable((By.XPATH, "//span[text()='Indicators']")))
    indicators_menu.click()

    # 2. 展开Upper Overlays菜单
    upper_overlays_menu = wait.until(EC.element_to_be_clickable((By.XPATH, "//span[text()='Upper Overlays']")))
    upper_overlays_menu.click()

    # 3. 等待并点击布林带复选框
    bollinger_checkbox = wait.until(EC.element_to_be_clickable((By.ID, "BBm")))
    bollinger_checkbox.click()

    # 等待图表更新(可选,根据实际情况调整等待时间)
    driver.implicitly_wait(2)

    # 保存截图
    screenshot_name = 'IEX.png'
    screenshot_path = os.path.join(screenshot_folder, screenshot_name)
    driver.save_screenshot(screenshot_path)

finally:
    # 关闭驱动
    driver.quit()

关键修正点

  • 使用显式等待(WebDriverWait)确保元素可交互后再操作,避免因页面加载慢导致的错误
  • 先依次展开「Indicators」和「Upper Overlays」菜单,让复选框处于可见状态
  • 改用新版headless模式(--headless=new),提升兼容性和渲染效果
  • 添加必要的Chrome启动参数,避免环境相关问题
  • 使用try-finally确保驱动正常关闭

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.04 15:25:33