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

Python主函数首次运行重复请求输入,二次运行恢复正常问题排查

解决首次运行时重复请求用户输入的问题

我编写了一个自动化脚本,会根据用户输入调用对应脚本的方法并传入参数。但主脚本首次运行时会两次请求用户输入信息,第二次及之后运行则正常工作。

主脚本代码

from spw import seaSec

# main function to call for pw resets
def menu ():
    con = ""
    
    while (con!= "no"):

        empID = input("What is the employee ID ")
        last4 = input("What are ") 
        answer = input("Which website would you like to go to ").lower()
        
        if answer == "sea":
            seaSec(empID, last4)
    
        #Will add once OmniCo script works
        #elif answer == "omni"
        #omniCo(empID, last4)
    
        con = input("Need help with anything else? (Enter y or n) ")

        if con == "n":
            quit()

menu()

seaSec函数代码

def seaSec(empID, last4):
    from **** import createTicket
    PATH = "C:\Program Files (x86)\chromedriver.exe"
    driver = webdriver.Chrome(PATH)

    driver.get("http://wodapp/SecurityServices/Admin/FindUser.aspx")
    driver.minimize_window()

    #Input employee ID input to verify
    ID = driver.find_element(By.ID, 'ContentPlaceHolder1_sapPersonnelNumberTextBox')
    ID.send_keys(empID)
    
    #submit information 
    Sub = driver.find_element(By.ID, "ContentPlaceHolder1_submitImageButton")
    Sub.click()


    #Hold SSN for verification, then compare to user input
    VER = driver.find_element(By.ID, 'ContentPlaceHolder1_userRepeater_editImageButton_0')
    VER.click()

    num= driver.find_element(By.ID, "ContentPlaceHolder1_").get_attribute('value')
    
while cont != "0":

        if (last4 == num):
            cont = 0
            break

        elif (last4 != num):
            print("Value does not match")
            return False


    firstName = driver.find_element(By.ID, "ContentPlaceHolder1_firstNameTextBox").get_attribute('value')
    lastName = driver.find_element(By.ID, "ContentPlaceHolder1_lastNameTextBox").get_attribute('value')

    fullName = firstName + " " + lastName

    select = Select(driver.find_element(By.ID, "ContentPlaceHolder1_homeParkDropDownList"))
    parkID = select.first_selected_option.text

    driver.back()

    choice = input("basic or xstore? ").lower()

    def spw(): 
       Reset = driver.find_element(By.ID, "ContentPlaceHolder1_userRepeater_resetPasswordImageButton_0")
       Reset.click()
       
       print(createTicket("pwd", parkID, empID, fullName, "Sea-Port" ))
        
       driver.quit()
    
    def xpw():
        xStore = driver.find_element(By.ID, "ContentPlaceHolder1_userRepeater_manageXStoreImageButton_0")
        xStore.click()

        choice = input("Unlock or PW? ").lower()

        if choice == "unlock":
            unlock = driver.find_element(By.ID, "ContentPlaceHolder1_unlockImageButton")
            unlock.click()

            print(createTicket("unlock", parkID, empID, fullName, "Xstore"))

        elif choice == "pw":
            reset = driver.find_element(By.ID, "ContentPlaceHolder1_resetPasswordImageButton")
            reset.click()

            print(createTicket("pwd", parkID, empID, fullName, "Xstore"))


    if choice == "basic":
        spw()
    elif choice == "xstore":
        xpw()
    
    driver.quit()

问题根源

核心问题是代码缩进错误:while cont != "0":这段循环代码原本属于seaSec函数内部,但现在是模块级别的全局代码。当首次导入spw模块时,Python会自动执行模块内的所有全局代码,这段错误缩进的循环会触发额外逻辑,导致主脚本的输入请求被重复触发。

此外还有几个次要问题影响脚本稳定性:

  • 驱动路径中的反斜杠未转义,可能导致路径解析错误
  • cont变量未初始化,会触发NameError
  • driver.quit()被重复调用,会导致WebDriver异常
  • SSN验证的元素ID不完整,无法正确定位元素

修复步骤及代码示例

1. 修正缩进错误

将while cont != "0":及其内部代码缩进,放入seaSec函数内部(紧跟在num = ...之后)。

2. 初始化变量与修复路径

  • 初始化cont变量为非"0"的值
  • 使用原始字符串或转义反斜杠修复驱动路径

3. 移除重复的驱动关闭操作

确保driver.quit()只被调用一次,避免重复关闭驱动。

4. 补全元素ID

将ContentPlaceHolder1_补全为实际的SSN后四位输入框ID。

修复后的seaSec函数:

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import Select
from **** import createTicket  # 替换为实际模块名

def seaSec(empID, last4):
    # 用原始字符串避免转义问题
    PATH = r"C:\Program Files (x86)\chromedriver.exe"
    driver = webdriver.Chrome(PATH)

    driver.get("http://wodapp/SecurityServices/Admin/FindUser.aspx")
    driver.minimize_window()

    # 输入员工ID验证
    ID = driver.find_element(By.ID, 'ContentPlaceHolder1_sapPersonnelNumberTextBox')
    ID.send_keys(empID)
    
    # 提交信息
    Sub = driver.find_element(By.ID, "ContentPlaceHolder1_submitImageButton")
    Sub.click()

    # 进入验证页面获取SSN后四位
    VER = driver.find_element(By.ID, 'ContentPlaceHolder1_userRepeater_editImageButton_0')
    VER.click()

    # 补全实际的元素ID
    num = driver.find_element(By.ID, "ContentPlaceHolder1_ssnLast4TextBox").get_attribute('value')
    
    # 初始化循环变量
    cont = "1"
    while cont != "0":
        if last4 == num:
            cont = "0"
            break
        else:
            print("Value does not match")
            driver.quit()
            return False

    # 获取用户姓名与园区ID
    firstName = driver.find_element(By.ID, "ContentPlaceHolder1_firstNameTextBox").get_attribute('value')
    lastName = driver.find_element(By.ID, "ContentPlaceHolder1_lastNameTextBox").get_attribute('value')
    fullName = f"{firstName} {lastName}"

    select = Select(driver.find_element(By.ID, "ContentPlaceHolder1_homeParkDropDownList"))
    parkID = select.first_selected_option.text

    driver.back()

    # 选择重置类型
    choice = input("basic or xstore? ").lower()

    def spw(): 
        Reset = driver.find_element(By.ID, "ContentPlaceHolder1_userRepeater_resetPasswordImageButton_0")
        Reset.click()
        print(createTicket("pwd", parkID, empID, fullName, "Sea-Port"))
        driver.quit()
    
    def xpw():
        xStore = driver.find_element(By.ID, "ContentPlaceHolder1_userRepeater_manageXStoreImageButton_0")
        xStore.click()

        sub_choice = input("Unlock or PW? ").lower()

        if sub_choice == "unlock":
            unlock = driver.find_element(By.ID, "ContentPlaceHolder1_unlockImageButton")
            unlock.click()
            print(createTicket("unlock", parkID, empID, fullName, "Xstore"))
        elif sub_choice == "pw":
            reset = driver.find_element(By.ID, "ContentPlaceHolder1_resetPasswordImageButton")
            reset.click()
            print(createTicket("pwd", parkID, empID, fullName, "Xstore"))
        driver.quit()

    if choice == "basic":
        spw()
    elif choice == "xstore":
        xpw()

主脚本微调(可选):

from spw import seaSec

# 密码重置主菜单
def menu():
    con = ""
    
    while con != "no":
        empID = input("What is the employee ID ")
        last4 = input("What are the last 4 digits of SSN? ") 
        answer = input("Which website would you like to go to ").lower()
        
        if answer == "sea":
            seaSec(empID, last4)
    
        # OmniCo脚本完成后添加
        # elif answer == "omni":
        #     omniCo(empID, last4)
    
        con = input("Need help with anything else? (Enter y or n) ").lower()

        if con in ["n", "no"]:
            break

menu()

修改完成后,首次运行主脚本时就不会出现重复请求输入的问题,脚本稳定性也会提升。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.24 23:24:56