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

如何创建带掩码的登录输入框,配合Selenium实现网页登录项手动填充?

实现带掩码的账号密码输入框并自动填充至网页登录区域

你可以用Python标准库tkinter快速实现带掩码的密码输入框,无需额外安装第三方库,直接和Selenium配合完成需求。以下是整合后的实现方案:


完整代码

from selenium import webdriver
from selenium.webdriver.common.by import By
import tkinter as tk
from tkinter import messagebox

def login_with_input():
    # 获取输入框中的账号密码
    input_username = entry_username.get().strip()
    input_password = entry_password.get().strip()
    
    if not input_username or not input_password:
        messagebox.showwarning("提示", "账号和密码不能为空")
        return
    
    # 启动浏览器并执行登录流程
    driver = webdriver.Chrome()
    try:
        driver.get("这里替换为你的目标登录页面URL")
        # 定位登录元素并填充信息
        username = driver.find_element(By.NAME, 'username')
        password = driver.find_element(By.NAME, 'password')
        acessar = driver.find_element(By.ID, 'btnLogin')
        
        username.send_keys(input_username)
        password.send_keys(input_password)
        acessar.click()
    except Exception as e:
        messagebox.showerror("错误", f"登录过程出现问题:{str(e)}")
        driver.quit()

# 创建GUI窗口
root = tk.Tk()
root.title("账号密码输入")
root.geometry("300x150")

# 账号输入区域
label_username = tk.Label(root, text="登录名:")
label_username.pack(pady=5)
entry_username = tk.Entry(root, width=30)
entry_username.pack(pady=2)

# 密码输入区域(带掩码)
label_password = tk.Label(root, text="密码:")
label_password.pack(pady=5)
entry_password = tk.Entry(root, width=30, show="*")  # show参数设置掩码字符
entry_password.pack(pady=2)

# 提交按钮
btn_submit = tk.Button(root, text="提交并登录", command=login_with_input)
btn_submit.pack(pady=10)

# 启动GUI循环
root.mainloop()

关键说明

  • tkinter是Python自带的GUI库,无需额外安装,直接导入即可使用
  • 密码输入框通过show="*"实现字符掩码,输入内容会以*显示,保护隐私
  • 点击「提交并登录」按钮后,会先校验输入是否为空,再启动Selenium流程,将输入的账号密码填充到网页对应位置并执行登录
  • 加入了异常捕获和弹窗提示,方便排查登录过程中出现的问题

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.01 23:00:25