Python Selenium自动注册谷歌账号遇ElementNotInteractableException错误求助
问题分析
你遇到的ElementNotInteractableException核心原因有两个:
- 用
EC.presence_of_element_located仅保证元素存在于DOM中,但不确保它可见、可交互。谷歌登录页面可能存在多个同名/同类型的密码输入框(比如隐藏的备用元素),你定位到的恰好是不可交互的那个。 - 邮箱输入环节写死了字符串
'email',而非你输入的变量email,这会导致邮箱验证失败,后续页面状态异常,密码框自然无法正常交互。
解决方案
针对上述问题,直接调整代码即可:
- 替换等待条件:将密码框的等待逻辑从
presence_of_element_located改为element_to_be_clickable,该条件会等待元素可见且可被点击,确保能正常输入内容。 - 修正邮箱输入错误:把
second_click.send_keys('email')改为second_click.send_keys(email),使用你输入的变量值而非固定字符串。 - 可选:更精准定位:若仍有问题,可结合父容器编写XPATH,避免定位到隐藏元素。
修改后的完整代码
from selenium import webdriver from selenium.webdriver.chrome.service import Service as ChromeService from webdriver_manager.chrome import ChromeDriverManager from selenium.webdriver.common.keys import Keys from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.support import expected_conditions as EC from selenium.webdriver.common.by import By email = input('Enter email : ') password = input('Enter password : ') options = webdriver.ChromeOptions() options.add_experimental_option("excludeSwitches", ["enable-automation"]) options.add_experimental_option('useAutomationExtension', False) options.add_argument('--disable-blink-features=AutomationControlled') options.add_argument('--start-maximized') # 最大化窗口,避免元素被遮挡 driver = webdriver.Chrome(service=ChromeService(ChromeDriverManager().install()), options=options) driver.get('https://google.com') # 等待并点击登录按钮 first_click = WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, "//span[@class='gb_Dd']"))) first_click.click() # 输入邮箱 second_click = WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, "//input[@id='identifierId']"))) second_click.send_keys(email) # 使用输入的邮箱变量 second_click.send_keys(Keys.RETURN) # 等待密码框并输入密码 third_click = WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//input[@name='Passwd']"))) third_click.send_keys(password) third_click.send_keys(Keys.RETURN)
额外注意事项
- 谷歌有严格的反自动化检测,即使调整代码,也可能触发人机验证,这种情况Selenium无法绕过,只能手动处理。
- 如果密码框仍无法交互,可尝试用
ActionChains先点击输入框再输入:
from selenium.webdriver.common.action_chains import ActionChains # 替换原密码输入部分 third_click = WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "//input[@name='Passwd']"))) ActionChains(driver).move_to_element(third_click).click().send_keys(password).perform()
内容的提问来源于stack exchange,提问作者Abdelrhman Samir
相关产品推荐
相关产品推荐

