python - Selenium cannot locate text field to populate - Stack Overflow

I am attempting to automate the process of filling out a web form requiring various pieces of informati

I am attempting to automate the process of filling out a web form requiring various pieces of information from a stored PDF to produce an insurance quote. The first field to be filled out is the "Applicant First Name" field as seen in the attached screenshot. However, I keep getting an error indicating that Selenium cannot locate the text field to populate. I am not sure about what to do now. Could anyone please help show me what I am doing wrong and describe the correct way to implement this? Screenshot of web form

This is the element that I am trying to access:

<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 of the element:

//*[@id="ctl00_ConPlaceHolder_FirstName"]

Full XPath of the element:

/html/body/form/div[6]/div/div[5]/div/table[1]/tbody/tr[5]/td[1]/font/input

Attached below is screenshot giving more context to the element:

Larger picture of the element

I am new to working with Selenium so apologies if I am missing something obvious.

MRE included directly below.

from selenium import webdriver
from selenium.webdrivermon.by import By
from selenium.webdrivermon.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

# Iterate through stored PDFs and extract the data to save. This data will be put in to text fields on the web form.

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)

# Iterate through the data and enter it into the text fields on the web form.
for index, row in quote_data.iterrows():
    # Enter applicant details
    time.sleep(5)  # Wait for elements to fully load
    
    # Previously tried methods (which failed) to find the text field for the corresponding labels starting with the "Applicant First Name" field.
    
    # 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"])
    
    # Enter text into the other text fields
    
    # Then submit quote request
    driver.find_element(By.NAME, "submitQuote").click()

Below are the lines that I have used to try to populate the text field. I expected that at least one of them would work and the text would populate, but none of them worked.

1.

WebDriverWait(driver, 20).until(EC.presence_of_element_located((By.NAME, "ctl00$ConPlaceHolder$FirstName"))).send_keys(row["First Name"])
WebDriverWait(driver, 20).until(EC.presence_of_element_located((By.ID, "ctl00_ConPlaceHolder_FirstName"))).send_keys(row["First Name"])
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"])
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"])

I would appreciate any help that you could kindly provide.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744718191a4589749.html

相关推荐

  • python - Selenium cannot locate text field to populate - Stack Overflow

    I am attempting to automate the process of filling out a web form requiring various pieces of informati

    20小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信