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

Selenium可点击搜索框但无法send_keys的问题求助

问题描述

能定位并点击目标搜索框,但无法通过send_keys输入内容,尝试多种元素定位方式甚至JavaScript都失败。以下是使用的Selenium代码片段及搜索框HTML结构:

代码片段

driver.find_element(By.XPATH, '(//div)[125]').click()
time.sleep(2)
driver.find_element(By.XPATH, '(//div)[125]').clear()
time.sleep(2)
driver.find_element(By.XPATH, "(//span)[125]").send_keys("abc")
time.sleep(1)
driver.find_element(By.XPATH, '(//div)[125]').send_keys(Keys.ENTER)

搜索框HTML结构

<div class="sc-bdVaJa hjHTBy sDash_fullwidth-select" style="--gap:8px; --template: initial;"><div class="ant-select sc-htoDjs iOndSp ant-select-single ant-select-show-arrow ant-select-open ant-select-show-search"><div class="ant-select-selector"><span class="ant-select-selection-search"><input type="search" autocomplete="off" class="ant-select-selection-search-input" role="combobox" aria-haspopup="listbox" aria-owns="rc_select_1_list" aria-autocomplete="list" aria-controls="rc_select_1_list" aria-activedescendant="rc_select_1_list_0" value="" id="rc_select_1" aria-expanded="true"></span><span class="ant-select-selection-placeholder">Search from Lahore</span></div><span class="ant-select-arrow" unselectable="on" aria-hidden="true" style="user-select: none;"><span role="img" class="anticon"><svg stroke="currentColor" fill="currentColor" stroke-width="0" viewBox="0 0 24 24" height="1.2em" width="1.2em" xmlns="http://www.w3.org/2000/svg"><path fill="none" d="M0 0h24v24H0z"></path><path d="M7.41 15.41L12 10.83l4.59 4.58L18 14l-6-6-6 6z"></path></svg></span></span></div></div>

<div class="ant-select sc-htoDjs iOndSp ant-select-single ant-select-show-arrow ant-select-open ant-select-show-search"><div class="ant-select-selector"><span class="ant-select-selection-search"><input type="search" autocomplete="off" class="ant-select-selection-search-input" role="combobox" aria-haspopup="listbox" aria-owns="rc_select_1_list" aria-autocomplete="list" aria-controls="rc_select_1_list" aria-activedescendant="rc_select_1_list_0" value="" id="rc_select_1" aria-expanded="true"></span><span class="ant-select-selection-placeholder">Search from Lahore</span></div><span class="ant-select-arrow" unselectable="on" aria-hidden="true" style="user-select: none;"><span role="img" class="anticon"><svg stroke="currentColor" fill="currentColor" stroke-width="0" viewBox="0 0 24 24" height="1.2em" width="1.2em" xmlns="http://www.w3.org/2000/svg"><path fill="none" d="M0 0h24v24H0z"></path><path d="M7.41 15.41L12 10.83l4.59 4.58L18 14l-6-6-6 6z"></path></svg></span></span></div>

<div class="ant-select-selector"><span class="ant-select-selection-search"><input type="search" autocomplete="off" class="ant-select-selection-search-input" role="combobox" aria-haspopup="listbox" aria-owns="rc_select_1_list" aria-autocomplete="list" aria-controls="rc_select_1_list" aria-activedescendant="rc_select_1_list_0" value="" id="rc_select_1" aria-expanded="true"></span><span class="ant-select-selection-placeholder">Search from Lahore</span></div>

::before

<span class="ant-select-selection-search"><input type="search" autocomplete="off" class="ant-select-selection-search-input" role="combobox" aria-haspopup="listbox" aria-owns="rc_select_1_list" aria-autocomplete="list" aria-controls="rc_select_1_list" aria-activedescendant="rc_select_1_list_0" value="" id="rc_select_1" aria-expanded="true"></span>

<input type="search" autocomplete="off" class="ant-select-selection-search-input" role="combobox" aria-haspopup="listbox" aria-owns="rc_select_1_list" aria-autocomplete="list" aria-controls="rc_select_1_list" aria-activedescendant="rc_select_1_list_0" value="" id="rc_select_1" aria-expanded="true">

<span class="ant-select-selection-search"><input type="search" autocomplete="off" class="ant-select-selection-search-input" role="combobox" aria-haspopup="listbox" aria-owns="rc_select_1_list" aria-autocomplete="list" aria-controls="rc_select_1_list" aria-activedescendant="rc_select_1_list_0" value="" id="rc_select_1" aria-expanded="true"></span>

<span class="ant-select-selection-placeholder">Search from Lahore</span>

::after 
</span>
解决方案

从HTML结构看,这是Ant Design带搜索功能的下拉选择框,核心问题是你没有定位到真正接收输入的<input>元素,而是操作了外层容器;同时(//div)[125]这种索引定位完全依赖页面元素顺序,极易失效。试试以下几种方法:

方法1:精准定位input元素(优先推荐)

通过input的class和placeholder属性定位,确保操作的是真正的输入控件:

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

# 等待input元素可点击,避免硬等待
search_input = WebDriverWait(driver, 10).until(
    EC.element_to_be_clickable((By.CSS_SELECTOR, 'input.ant-select-selection-search-input[placeholder="Search from Lahore"]'))
)

# 激活输入框并操作
search_input.click()
search_input.clear()
search_input.send_keys("abc")
search_input.send_keys(Keys.ENTER)

方法2:JavaScript强制注入输入

如果直接send_keys无效,可通过JS修改input值并触发组件感知的事件:

search_input = WebDriverWait(driver, 10).until(
    EC.presence_of_element_located((By.CSS_SELECTOR, 'input.ant-select-selection-search-input[placeholder="Search from Lahore"]'))
)

# 用JS设置输入值
driver.execute_script("arguments[0].value = 'abc';", search_input)
# 触发input事件,让组件识别内容变化
driver.execute_script("arguments[0].dispatchEvent(new Event('input'));", search_input)
# 回车确认
search_input.send_keys(Keys.ENTER)

方法3:先展开下拉框再输入

如果input初始状态不可见,需先点击外层容器展开下拉:

# 点击下拉容器展开选项
select_container = WebDriverWait(driver, 10).until(
    EC.element_to_be_clickable((By.CSS_SELECTOR, 'div.ant-select-show-search'))
)
select_container.click()

# 等待input出现并操作
search_input = WebDriverWait(driver, 10).until(
    EC.element_to_be_clickable((By.CSS_SELECTOR, 'input.ant-select-selection-search-input'))
)
search_input.send_keys("abc")
search_input.send_keys(Keys.ENTER)

注意事项

  • 绝对不要使用(//div)[125]这类索引定位,页面结构稍有变动就会失效;
  • 用WebDriverWait替代time.sleep(),等待元素状态变化,提升代码稳定性;
  • 若页面有多个同类型组件,可结合id或外层容器的专属class进一步缩小定位范围。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.09 12:30:54