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

使用Python+Selenium向文本框输入值时遇问题求助

问题:使用Selenium Python无法向文本框输入值

尝试多种方法向目标文本框输入邮编11415均失败,以下是相关信息:

目标文本框HTML

<input type="text" class="form-control" name="location" id="location"  placeholder="Zip Code or City, State (e.g, 12345 or New York, NY)">&nbsp;&nbsp;<i class="active fa fa-check-circle"></i>

尝试的输入代码

WebDriverWait(driver, 20).until(EC.element_to_be_clickable(By.XPATH, "//input[@class='form-control' and @id='location']")).send_keys("11415")

完整代码

import selenium
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.chrome.service import Service
import time
import os
from selenium.webdriver.common.action_chains import ActionChains

from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC

from selenium.webdriver.remote.webelement import WebElement
from selenium.webdriver.common.keys import Keys


s=Service('/Users/[name]/Desktop/chromedriver')

chromeOptions = Options()
chromeOptions.headless = False
driver = webdriver.Chrome(service=s, options=chromeOptions)

list_data = []
    
def initalize_browser():
    # load website
    driver.get("https://www.fairhealthconsumer.org/")
    driver.maximize_window()

    driver.find_element(By.XPATH, '//button[@class="mx-auto green-btn btnHref"]').click()

    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, ".//*[@for='radio3']/span"))).click()
    driver.find_element(By.XPATH, ".//*[@for='radio3']//span[@class='pt-2 border-top border-white']").click()
    
    # inputElement = driver.find_element_by_id("location")
    # inputElement.send_keys('11415')
    
    # inputElement.send_keys(Keys.ENTER)

    driver.find_element(By.XPATH, '//button[@class="button custom-bgcolor-secondary next selected"]').click()

    # driver.find_element(By.XPATH, '//*[@id="location"]').click()
    # driver.find_element(By.XPATH,'//text[@id="location"]').send_keys("11415")

    # WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.ID, 'location'))).send_keys('11415')
    # button_to_be_clicked = browser.find_element(By.ID, 'button-submit')
    # button_to_be_clicked.click()

    WebDriverWait(driver, 20).until(EC.element_to_be_clickable(By.XPATH, "//input[@class='form-control' and @id='location']")).send_keys("11415")

    print(driver.page_source)

initalize_browser()


driver.quit

解决方案

  1. 简化元素定位:使用By.ID定位,id="location"是唯一标识,比XPath更可靠且简洁。
  2. 等待元素可见:替换element_to_be_clickable为visibility_of_element_located,确保元素完全渲染可见后再操作。
  3. 激活并清除输入框:先点击输入框激活,再清除可能存在的默认内容,最后输入值。

修改后的输入代码片段:

# 等待元素可见
location_input = WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.ID, 'location')))
# 激活输入框并清除内容
location_input.click()
location_input.clear()
# 输入邮编
location_input.send_keys("11415")

将这段代码替换原代码中对应的输入部分即可解决问题。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.07 19:30:55