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

Selenium Python报错ElementNotInteractableException:输入框点击有效但无法输入

问题分析

你遇到的ElementNotInteractableException是因为你定位并操作的是<label>元素,而非实际的输入框<input>。点击<label>会触发关联的输入框获得焦点,但<label>本身不支持文本输入,所以调用send_keys()会报错。

解决方案

修改定位逻辑,直接找到名字输入框对应的<input>元素进行文本输入,同时建议用显式等待替代time.sleep(),提升代码稳定性。

修正后的代码

import time
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
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

s = Service("C:/Users/TUL/Desktop/cd/cd.exe")
driver = webdriver.Chrome(service=s)
driver.get('https://www.universityliving.com/')
driver.maximize_window()

# 关闭弹窗
WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, '/html/body/div[3]/div/div/div[2]/button'))).click()

# 点击第一个房源卡片
WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, '/html/body/div[1]/div/div[2]/div[3]/div/div/a[1]/div/div/div'))).click()

# 切换到第二个窗口
WebDriverWait(driver, 10).until(EC.number_of_windows_to_be(2))
driver.switch_to.window(driver.window_handles[1])

# 点击目标房源图片
WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, "//div[@class='slick-slide slick-active slick-current']//div//img[@alt='Urbanest Carlton Melbourne']"))).click()

# 切换到第三个窗口
WebDriverWait(driver, 10).until(EC.number_of_windows_to_be(3))
driver.switch_to.window(driver.window_handles[2])

# 选择房型
WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, '/html/body/div[2]/div/div[2]/div[3]/div[2]/div/div[2]/div/div[3]/div/div/div[3]/div/div[3]/div/ul/li[2]/p'))).click()

# 定位名字输入框并发送文本
name_input = WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, '//*[@id="enquiryForm"]//div[contains(@class, "form-group")][1]//input')))
name_input.send_keys('h')

time.sleep(3)
driver.quit()

关键修改点

  1. 替换定位对象:从<label>改为实际的<input>元素,确保可以调用send_keys()方法。
  2. 使用显式等待:通过WebDriverWait等待元素可交互,替代固定时长的time.sleep(),避免因页面加载慢导致的元素定位失败。
  3. 优化XPATH:尽量使用相对路径替代绝对路径,减少页面结构变化带来的影响。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.11 18:01:25