一个用于自动化操作 MetaMask 钱包的工具,支持批量导入钱包、连接网站、处理交互等操作。
- 批量创建和导入钱包
- 多线程并行处理
- 自动处理 MetaMask 弹窗和交互
- 支持多种网络切换
- 智能错误处理和日志记录
- 克隆仓库:
git clone <repository_url>
cd Metamask_Auto
- 安装依赖:
pip install -r requirements.txt
- 确保 extensions 目录下有正确版本的 MetaMask 扩展。
用于打开新标签页或切换窗口
# 打开新URL
last_handle = bot.open_and_switch_to_window(driver, url="https://example.com")
# 切换到指定窗口
bot.open_and_switch_to_window(driver, window_handle=last_handle)
处理 MetaMask 弹窗(包括下一步和连接按钮)
# 处理弹窗并自动切回主窗口
main_window = driver.current_window_handle
success = bot.handle_metamask_popup(main_window)
# 仅处理弹窗不切回主窗口
success = bot.handle_metamask_popup()
处理 MetaMask 通知(包括连接请求和签名请求)
bot.handle_metamask_notification()
切换网络
bot.switch_network(driver, "BSC Mainnet")
等待元素出现并返回
# 等待特定元素出现
element = bot.wait_for_element("//button[contains(text(), '连接钱包')]")
# 自定义超时时间
element = bot.wait_for_element("//div[@class='target']", timeout=15)
- 窗口切换规范
# 保存主窗口句柄
main_window = driver.current_window_handle
# 执行可能触发新窗口的操作
some_operation()
# 处理弹窗
bot.handle_metamask_popup(main_window) # 自动切回主窗口
# 验证操作结果
verify_operation()
- 错误处理规范
try:
# 执行操作
result = bot.handle_metamask_popup(main_window)
if not result:
logger.error("操作失败")
return False
except Exception as e:
logger.error(f"发生错误: {str(e)}")
return False
- 等待时间处理
# 短暂操作等待
time.sleep(2)
# 页面加载等待
time.sleep(5)
# 使用显式等待
element = bot.wait_for_element(xpath, timeout=10)
- 始终在操作前保存主窗口句柄
- 使用 try-except 处理所有可能的异常
- 合理设置等待时间,避免操作过快
- 验证每个关键操作的结果
- 使用日志记录重要信息,但避免记录敏感数据
- Fork 本仓库
- 创建特性分支
- 提交更改
- 发起 Pull Request
本项目采用 MIT 许可证。详见 LICENSE 文件。
- 推特:https://x.com/KingSmile88
- 微信:yufeng8138
- DC社区:https://discord.gg/gTQ3uhxCFs
- 首先运行
gen_address.py
生成账户:
python src/gen_address.py
- 然后运行主程序:
python src/main.py
- 请确保已安装 Chrome 浏览器
- 需要正确配置 MetaMask 扩展
- 建议在运行前备份重要数据