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

Selenium WebDriver无法操控MetaMask扩展,点击同意按钮无响应求助

问题:Selenium无法识别并点击MetaMask条款复选框

已将MetaMask扩展下载并添加至Chrome浏览器,启动Selenium WebDriver后会跳转至扩展页面,但尝试点击"我同意MetaMask条款"复选框时无任何反应,WebDriver甚至无法识别该元素。(忽略Brave浏览器截图,ChromeDriver表现一致)

环境配置

  • Python 3.10.6
  • Chrome及ChromeDriver 115
  • Linux Mint
  • MetaMask扩展10.34.2

原代码

import time
import os
from selenium.webdriver.common.by import By
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
import requests
import shutil


if __name__ == '__main__':
    # Downloading Metamask extension
    file_path = os.getcwd()
    url = "https://github.com/MetaMask/metamask-extension/releases/download/v10.34.2/metamask-chrome-10.34.2.zip"
    local_filename = file_path + '/' + url.split('/')[-1]

    with requests.get(url, stream=True) as r:
        with open(local_filename, 'wb') as f:
            shutil.copyfileobj(r.raw, f)


    # Chrome options configuration
    chrome_options = webdriver.ChromeOptions()
    chrome_options.add_argument('--ignore-certificate-errors-spki-list')
    chrome_options.add_argument('--ignore-certificate-errors')
    chrome_options.add_argument('--ignore-ssl-errors')
    chrome_options.add_argument('--no-sandbox')
    chrome_options.add_argument("--disable-blink-features=BlockCredentialedSubresources")
    chrome_options.add_argument("--disable-gpu")
    chrome_options.add_experimental_option("excludeSwitches", ["enable-automation"])
    chrome_options.add_argument('--disable-blink-features=AutomationControlled')
    chrome_options.add_argument("--disable-setuid-sandbox")
    chrome_options.add_argument("--disable-infobars")
    chrome_options.add_argument('--disable-software-rasterizer')
    # Adding extension to Chrome
    chrome_options.add_extension("metamask-chrome-10.34.2.zip")
    # Location to the Chrome binary (for Linux)     
    chrome_options.binary_location = "/usr/bin/google-chrome-stable"
    s = Service(
        executable_path='MY_FULL_EXECUTABLE_PATH'
    )
    driver = webdriver.Chrome(
        service=s,
        options=chrome_options
    )
    # Waiting 8 seconds for a page to download fully
    time.sleep(8)
    # The "I agree to Metamask terms" checkbox I want to click
    metamask_checkbox = driver.find_element(By.CSS_SELECTOR, '#onboarding__terms-checkbox')
    metamask_checkbox.click()
    time.sleep(5)
    driver.quit()

解决方案

1. 切换到MetaMask的窗口

启动扩展后,Chrome会新开窗口承载MetaMask初始化页面,WebDriver默认停留在原窗口,需手动切换:

time.sleep(3)
# 获取所有窗口句柄,切换到最后打开的窗口(即MetaMask窗口)
window_handles = driver.window_handles
driver.switch_to.window(window_handles[-1])

2. 替换固定等待为显式等待

time.sleep()无法确保元素完全加载,改用Selenium显式等待,保证元素可点击后再操作:

from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

# 最多等待15秒,直到复选框可点击
metamask_checkbox = WebDriverWait(driver, 15).until(
    EC.element_to_be_clickable((By.CSS_SELECTOR, '#onboarding__terms-checkbox'))
)
metamask_checkbox.click()

3. 验证元素选择器有效性

MetaMask版本更新可能修改元素ID或结构,打开Chrome开发者工具(F12)重新定位复选框,确认选择器是否为#onboarding__terms-checkbox,若已变更则替换为新的选择器(如XPath)。

4. 检查是否存在iframe嵌套

部分MetaMask页面元素会放在iframe内,需先切换到对应iframe再查找元素:

# 查找并切换到MetaMask的iframe(根据实际元素调整选择器)
iframe = WebDriverWait(driver, 10).until(
    EC.presence_of_element_located((By.XPATH, "//iframe[contains(@src, 'metamask')]"))
)
driver.switch_to.frame(iframe)

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.14 15:17:52