Python自动化手动数据录入:无法识别输入字段问题求助
问题描述
- 业务流程:从会计软件提取数据,筛选错误数据行并生成对应正确数据行(错误行与正确行交替排列),需将这些数据录入另一款软件,本应实现自动化。
- 脚本现状:编写的Python脚本已完成Web应用登录(包含MFA等待逻辑),并导航至数据录入页面,但无法识别任何数据录入字段,触发超时错误。已添加调试逻辑并生成source.html用于排查,脚本内容如下:
import time import pandas as pd 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 from webdriver_manager.chrome import ChromeDriverManager from selenium.common.exceptions import TimeoutException, NoSuchElementException, ElementNotInteractableException # Initialize Chrome WebDriver using WebDriver Manager driver = webdriver.Chrome(service=Service(ChromeDriverManager().install())) # Navigate to the login page driver.get('starting url') # Locate the input fields and enter credentials cruzid_field = driver.find_element(By.NAME, 'j_username') password_field = driver.find_element(By.NAME, 'j_password') # Enter your credentials cruzid_field.send_keys('actual username') password_field.send_keys('actual password') # Click the login button login_button = driver.find_element(By.NAME, '_eventId_proceed') login_button.click() # After successful login, locate the search input field and enter the search term try: WebDriverWait(driver, 60).until(EC.url_contains('next url')) print("Logged in successfully!") # Locate the search input field using its ID search_field = WebDriverWait(driver, 20).until( EC.presence_of_element_located((By.ID, 'search-landing')) ) search_field.send_keys('data entry code') # Simulate pressing Enter to initiate the search search_field.send_keys(Keys.RETURN) # Switch to the iframe containing the content print("Switching to iframe") iframe = WebDriverWait(driver, 20).until( EC.presence_of_element_located((By.ID, 'bannerHS')) ) driver.switch_to.frame(iframe) print("Switched to iframe") # Verify the current context is the iframe current_frame = driver.execute_script("return self.name") print(f"Current iframe context: {current_frame}") # Wait for the tab-container element to be present after search (up to 120 seconds) print("Waiting for the tab-container element to be present") tab_container = WebDriverWait(driver, 120).until( EC.presence_of_element_located((By.ID, 'tab-container')) ) print("Tab-container element is present, search completed!") # Attempt to locate the "Go" button using ID print("Attempting to locate the Go button by ID") go_button = WebDriverWait(driver, 20).until( EC.element_to_be_clickable((By.ID, 'frames6')) ) go_button.click() print("Go button clicked successfully!") # Wait for the pnlFgbjvch1Canvas element to appear print("Waiting for pnlFgbjvch1Canvas element to be present") pnl_element = WebDriverWait(driver, 60).until( EC.presence_of_element_located((By.ID, 'pnlFgbjvch1Canvas')) ) print("pnlFgbjvch1Canvas element is present") # Adding a brief delay to ensure the element is ready for interaction time.sleep(2) # Pause for manual date and document total entry (PROBLEM 1) input("Please enter the transaction date and document total manually, then press Enter to continue...") # Wait for the specific div elements to be present (PROBLEM 2) print("Waiting for the view div element to be present") view_div = WebDriverWait(driver, 60).until( EC.presence_of_element_located((By.ID, 'view_16d82cc3-d839-4611-aaf5-6c06ddf7b162')) ) print("View div element is present") print("Waiting for the collapsible panel (rect145) to be present") collapsible_panel = WebDriverWait(driver, 60).until( EC.presence_of_element_located((By.ID, 'rect145')) ) print("Collapsible panel (rect145) is present") # Perform the tab action to move to the next field print("Performing tab action") body = driver.find_element(By.TAG_NAME, 'body') body.send_keys(Keys.TAB) print("Waiting for the frames29 panel to be present") frames_panel = WebDriverWait(driver, 60).until( EC.presence_of_element_located((By.ID, 'frames29')) ) print("Frames29 panel is present") # Click the input field with ID inp:fgbjvcdRuclCode print("Locating the RUCL Code input field") rucl_code_input = WebDriverWait(driver, 20).until( EC.element_to_be_clickable((By.ID, 'inp:fgbjvcdRuclCode')) ) rucl_code_input.click() print("RUCL Code input field clicked") # Read the 4-character code from the Excel file print("Reading the 4-character code from the Excel file") df = pd.read_excel('file/path') code = df.iloc[0, 0] print(f"Code read from Excel: {code}") # Enter the code into the input field print("Entering the code into the RUCL Code input field") rucl_code_input.send_keys(code) print("Complete") time.sleep(30) except (TimeoutException, NoSuchElementException, ElementNotInteractableException) as e: print(f"Error occurred: {e}") # Close the browser driver.quit()
替换代码后的异常情况
将问题1的手动录入部分替换为以下XPath定位交易日期字段的代码,第一次运行成功并切换到下一个字段,但第二次运行时超时,无法识别并点击交易日期字段:
# Use a more robust XPath to locate the transaction date input field print("Locating the transaction date input field") transaction_date_input = WebDriverWait(driver, 20).until( EC.presence_of_element_located( (By.XPATH, '//input[contains(@id, "fgbjvchTransDate") and @name="fgbjvchTransDate"]')) ) print("Transaction date input field located") # Ensure the element is visible and interactable WebDriverWait(driver, 20).until( EC.visibility_of(transaction_date_input) ) WebDriverWait(driver, 20).until( EC.element_to_be_clickable(transaction_date_input) ) # Click into the transaction date input field print("Clicking the transaction date input field") transaction_date_input.click() # Perform the tab action print("Performing tab action") transaction_date_input.send_keys(Keys.TAB)
附:交易日期字段界面截图及源码截图
内容的提问来源于stack exchange,提问作者hi.jrdn
相关产品推荐
相关产品推荐

