Windows下Firefox+Selenium输入账号密码遇ElementNotInteractableException求助
解决Selenium搭配Firefox时无法输入账号密码的ElementNotInteractableException问题
问题场景
在Windows系统中使用Selenium WebDriver搭配Firefox浏览器访问目标登录页时,尝试通过ID定位邮箱和密码输入框并调用send_keys()方法时,出现ElementNotInteractableException: Message: Element is not reachable by keyboard错误,即使尝试了ID、CSS、Class等多种定位方式仍无法解决。
原代码
from selenium import webdriver from selenium.webdriver.common.by import By from selenium.webdriver.common.keys import Keys from selenium.webdriver.common.action_chains import ActionChains from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.support import expected_conditions as EC import time # Driver Configuration browser = webdriver.Firefox(executable_path=r'C:\geckodriver.exe') browser.get('https://b2b.bluewaycorp.com/index.php/customer/account/login/') time.sleep(4) # Input User username_el = browser.find_element_by_id("email") username_el.send_keys('user_mail@gmail.es') # ERROR WHEN I TRY TO INPUT TEXT!! time.sleep(0.5) # Input Password password_el = browser.find_element_by_id("pass") password_el.send_keys("password") # ERROR WHEN I TRY TO INPUT TEXT!! time.sleep(0.5) submit_btn_el2 = browser.find_element_by_xpath("/html/body/div[1]/main/div[3]/div/div[2]/div[1]/div[2]/form/fieldset/div[4]/div[1]/button/span") time.sleep(1) submit_btn_el2.click()
解决方案
以下是几种可行的修复方式,按优先级尝试:
1. 替换强制等待为显式等待,确保元素可交互
原代码用time.sleep()做强制等待,无法保证元素真正就绪。改用显式等待,等待元素满足可交互状态后再操作:
from selenium import webdriver from selenium.webdriver.common.by import By from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.support import expected_conditions as EC # Driver Configuration browser = webdriver.Firefox(executable_path=r'C:\geckodriver.exe') browser.get('https://b2b.bluewaycorp.com/index.php/customer/account/login/') # 显式等待邮箱输入框可交互,超时时间10秒 username_el = WebDriverWait(browser, 10).until( EC.element_to_be_clickable((By.ID, "email")) ) username_el.send_keys('user_mail@gmail.es') # 显式等待密码输入框可交互 password_el = WebDriverWait(browser, 10).until( EC.element_to_be_clickable((By.ID, "pass")) ) password_el.send_keys("password") # 显式等待提交按钮可交互后点击 submit_btn = WebDriverWait(browser, 10).until( EC.element_to_be_clickable((By.XPATH, "//button[@type='submit']")) ) submit_btn.click()
注意:提交按钮的XPath改用更稳定的//button[@type='submit'],避免绝对路径因页面结构变化失效。
2. 若元素被遮挡,使用ActionChains先聚焦再输入
如果输入框被弹窗、浮层遮挡(即使视觉上看不到),可通过ActionChains先移动到元素上再输入:
from selenium.webdriver.common.action_chains import ActionChains # 等待元素出现后 username_el = WebDriverWait(browser, 10).until( EC.presence_of_element_located((By.ID, "email")) ) # 移动到元素位置并点击激活 ActionChains(browser).move_to_element(username_el).click().send_keys('user_mail@gmail.es').perform() # 密码框同理 password_el = WebDriverWait(browser, 10).until( EC.presence_of_element_located((By.ID, "pass")) ) ActionChains(browser).move_to_element(password_el).click().send_keys("password").perform()
3. 执行JavaScript直接设置输入框值
如果前两种方法无效,可绕过Selenium的交互限制,直接通过JS修改元素的value属性:
username_el = WebDriverWait(browser, 10).until( EC.presence_of_element_located((By.ID, "email")) ) browser.execute_script("arguments[0].value = 'user_mail@gmail.es';", username_el) password_el = WebDriverWait(browser, 10).until( EC.presence_of_element_located((By.ID, "pass")) ) browser.execute_script("arguments[0].value = 'password';", password_el)
4. 检查Firefox和Geckodriver版本兼容性
确保你的Firefox浏览器版本和Geckodriver版本匹配:
- 查看Firefox版本:打开Firefox → 右上角菜单 → 帮助 → 关于Firefox
- 下载对应版本的Geckodriver:根据Firefox版本选择适配的Geckodriver(比如Firefox 115+对应Geckodriver 0.33+)
内容的提问来源于stack exchange,提问作者Pablo R
相关产品推荐
相关产品推荐

