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

Selenium无法定位并填充保险报价表单中投保人名字输入框的问题求助

Selenium无法定位并填充保险报价表单中投保人名字输入框的问题求助

我现在正在尝试自动化完成一个保险报价表单的填写流程——需要从存储的PDF文件里提取各类信息,然后填入网页表单中。第一个要填充的是**投保人名字(Applicant First Name)**输入框,但Selenium一直报错,说找不到这个文本框。我现在有点摸不着头脑,能不能帮我看看我哪里做错了,以及正确的实现方式应该是怎样的?

目标输入框的元素代码

<input name="ctl00$ConPlaceHolder$FirstName" type="text" id="ctl00_ConPlaceHolder_FirstName" maxlength="30" class="fdata" onmouseover="showContents(this,10)" onblur="makeUpperCase(this);" onchange="enableCreditButton()" onfocus="this.select()" style="width: 100%; visibility: visible; background-color: rgb(255, 222, 161);" size="15" title="">

元素的定位路径

  • XPath://*[@id="ctl00_ConPlaceHolder_FirstName"]
  • Full XPath:/html/body/form/div[6]/div/div[5]/div/table[1]/tbody/tr[5]/td[1]/font/input

我的最小可复现代码(MRE)

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
import time
import pandas as pd
import pdfplumber
import smtplib
from email.message import EmailMessage

# 遍历存储的PDF并提取数据,后续要填入表单
quote_data = []
for pdf_file in pdf_files:
    text = extract_pdf_data(pdf_file)
    quote_entry = {
        "First Name": text.split("First Name:")[1].split("\n")[0].strip() if "First Name:" in text else "N/A",
        "Last Name": text.split("Last Name:")[1].split("\n")[0].strip() if "Last Name:" in text else "N/A",
        "Phone": text.split("Insured Phone:")[1].split("\n")[0].strip() if " Insured Phone:" in text else "N/A",
        "Email": text.split("Insured Email:")[1].split("\n")[0].strip() if "Insured Email:" in text else "N/A",
        "Address": text.split("Street Address:")[1].split("\n")[0].strip() if " Street Address:" in text else "N/A",
        "ZIP": text.split("Zip Code:")[1].split("\n")[0].strip() if "Zip Code:" in text else "N/A",
        "County": text.split("County:")[1].split("\n")[0].strip() if "County:" in text else "N/A",
        "Year Built": text.split("Year Built:")[1].split("\n")[0].strip() if "Year Built:" in text else "N/A",
        "Square Footage": text.split("Total Square Footage:")[1].split("\n")[0].strip() if "Total Square Footage:" in text else "N/A",
        "Stories": text.split("Number Of Stories:")[1].split("\n")[0].strip() if "Number Of Stories:" in text else "N/A",
        "Bedrooms": text.split("Number Of Bedrooms:")[1].split("\n")[0].strip() if "Number Of Bedrooms:" in text else "N/A",
        "Bathrooms": text.split("Number of Bathrooms:")[1].split("\n")[0].strip() if "Number of Bathrooms:" in text else "N/A",
        "Primary Use": text.split("Primary Use:")[1].split("\n")[0].strip() if "Primary Use:" in text else "N/A",
        "Construction Type": text.split("Construction Type:")[1].split("\n")[0].strip() if "Construction Type:" in text else "N/A",
        "Roof Type": text.split("Roof Type:")[1].split("\n")[0].strip() if "Roof Type:" in text else "N/A"
    }
    quote_data.append(quote_entry)

quote_data = pd.DataFrame(quote_data)

# 遍历数据填入表单
for index, row in quote_data.iterrows():
    # 等待元素加载
    time.sleep(5)
    
    # 以下是我尝试过但都失败的定位方式(针对投保人名字输入框)
    # 1. 
    # WebDriverWait(driver, 20).until( EC.presence_of_element_located((By.NAME, "ctl00$ConPlaceHolder$FirstName")) ).send_keys(row["First Name"])
    
    # 2.
    # WebDriverWait(driver, 20).until( EC.presence_of_element_located((By.ID, "ctl00_ConPlaceHolder_FirstName")) ).send_keys(row["First Name"])
    
    # 3.
    # first_name_field = WebDriverWait(driver, 20).until( EC.presence_of_element_located((By.XPATH, "//input[@id='ctl00_ConPlaceHolder_FirstName']")) ) first_name_field.send_keys(row["First Name"])
    
    # 4.
    # first_name_field = WebDriverWait(driver, 30).until( EC.presence_of_element_located((By.XPATH, "/html/body/form/div[6]/div/div[5]/div/table[1]/tbody/tr[5]/td[1]/font/input")) ) first_name_field.send_keys(row["First Name"])
    
    # 其他输入框填写逻辑
    
    # 提交报价请求
    driver.find_element(By.NAME, "submitQuote").click()

我单独尝试过的定位填充代码

我原本以为下面这些方法至少有一个能成功填充输入框,但全部都失败了:

  1. 使用name属性定位:
WebDriverWait(driver, 20).until(EC.presence_of_element_located((By.NAME, "ctl00$ConPlaceHolder$FirstName"))).send_keys(row["First Name"])
  1. 使用ID属性定位:
WebDriverWait(driver, 20).until(EC.presence_of_element_located((By.ID, "ctl00_ConPlaceHolder_FirstName"))).send_keys(row["First Name"])
  1. 基于ID的XPath定位:
first_name_field = WebDriverWait(driver, 20).until(EC.presence_of_element_located((By.XPATH, "//input[@id='ctl00_ConPlaceHolder_FirstName']"))) 
first_name_field.send_keys(row["First Name"])
  1. 使用Full XPath定位:
first_name_field = WebDriverWait(driver, 30).until(EC.presence_of_element_located((By.XPATH, "/html/body/form/div[6]/div/div[5]/div/table[1]/tbody/tr[5]/td[1]/font/input"))) 
first_name_field.send_keys(row["First Name"])

我是Selenium的新手,如果有什么明显的遗漏还请大家见谅,非常感谢大家能提供帮助!

备注:内容来源于stack exchange,提问作者zpolak

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.04.13 18:54:36