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

使用Python Selenium自动化登录mirentapp网站失败问题排查

Selenium自动化登录失败问题排查与解决

问题背景

使用Python Selenium自动化完成https://mirentapp.app/(测试环境:https://qa.mirentapp.net/)的登录流程,目标是输入凭据后获取local storage中的用户令牌,但登录步骤始终停滞,无法定位失败原因。

简化代码

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

# Initialize the WebDriver (assuming Chrome in this example)
options = webdriver.ChromeOptions()
options.add_argument("--disable-web-security")
options.add_argument("--user-data-dir=/path/to/chrome/profile")
driver = webdriver.Chrome(service=Service(ChromeDriverManager().install()))

# Navigate to the page where you need to perform the login
driver.get("https://qa.mirentapp.net/")
# Wait for the username field to be present
WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.ID, "email")))

driver.find_element(By.ID, "email").send_keys("YOUR_EMAIL")
password_field = driver.find_element(By.ID, "password")
password_field.send_keys("YOUR_PASSWORD")
password_field.send_keys(Keys.RETURN)

已尝试操作

  • 验证登录凭据的正确性
  • 确认邮箱和密码输入框的ID无误
  • 检查是否有弹窗或警告框阻碍登录

问题解答

1. Selenium登录失败的常见陷阱

  • 元素交互时机错误:presence_of_element_located仅确保元素存在,但元素可能还未处于可交互状态,此时输入或触发操作会无效
  • 动态渲染页面的延迟:使用React/Vue等框架的页面,元素DOM存在但事件绑定未完成,导致操作无法触发登录逻辑
  • 反爬机制拦截:网站检测到Selenium特征(如navigator.webdriver标记),直接拦截登录请求或禁用操作
  • 触发方式错误:回车键可能未绑定登录提交逻辑,需点击登录按钮而非回车
  • 浏览器上下文异常:自定义profile或无痕模式导致Cookie、local storage无法正常读写
  • 网络延迟:页面未完全加载时执行输入操作,导致内容未真正填入字段

2. --user-data-dir选项的影响

大概率相关,可能的原因:

  • 指定的profile路径不存在或无读写权限,Chrome无法加载正常会话,影响登录后的令牌存储
  • 旧profile残留会话数据,导致新登录请求被拦截或覆盖
  • --disable-web-security与--user-data-dir冲突,改变了Chrome对local storage、跨域请求的处理逻辑,导致令牌无法写入
  • 自定义profile禁用了JavaScript或Cookie,直接导致登录逻辑无法执行

3. 调试与解决建议

调试步骤

  • 开启开发者工具自动打开:添加options.add_argument("--auto-open-devtools-for-tabs"),查看Network面板的登录请求状态、Console面板的JS报错
  • 截图确认状态:在关键步骤后添加driver.save_screenshot("login_check.png"),检查页面显示、输入框内容是否正确
  • 临时延迟验证:在输入凭据和触发登录之间添加time.sleep(2)(仅用于调试),确认元素已完全可交互
  • 验证输入内容:输入后打印print(driver.find_element(By.ID, "email").get_attribute("value")),确认内容是否成功填入

解决措施

  • 优化等待条件:将presence_of_element_located替换为element_to_be_clickable,确保元素可交互
    WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.ID, "email")))
    
  • 更换登录触发方式:定位登录按钮并点击,替代回车键提交
    login_button = WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, "//button[@type='submit']")))
    login_button.click()
    
  • 绕过反爬检测:添加参数隐藏Selenium特征
    options.add_argument("--disable-blink-features=AutomationControlled")
    options.add_experimental_option("excludeSwitches", ["enable-automation"])
    options.add_experimental_option('useAutomationExtension', False)
    
  • 修复profile配置:要么移除--user-data-dir使用默认profile,要么确保路径正确且有权限,同时移除--disable-web-security(非必要不要禁用)
  • 获取local storage令牌:登录成功后通过JS脚本提取
    user_token = driver.execute_script("return window.localStorage.getItem('user_token');")
    print(user_token)
    
  • 检查请求参数:查看登录接口是否需要CSRF令牌等额外参数,若有需先获取该参数再提交登录请求

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.01 02:44:52