You need to enable JavaScript to run this app. 使用Python Selenium实现自动化任务:如何导入CSV批量创建申请人
优惠活动
大模型
产品
解决方案
定价
更多

使用Python Selenium实现自动化任务:如何导入CSV批量创建申请人

Selenium脚本批量导入CSV申请人实现方案

前置准备

首先准备存储申请人信息的CSV文件,命名为applicants.csv,和脚本放在同一目录,格式如下:

last_name,first_name,email
freeman,seth,sfreeman@first.net
smith,john,jsmith@first.net
lee,amy,alee@first.net

表头必须包含last_name(姓氏)、first_name(名字)、email(邮箱)三个字段,每行对应一个申请人信息。

代码修改方案

Python自带csv处理模块,无需额外安装,只需调整代码结构,将仅需执行一次的初始化、登录逻辑保留,单个申请人录入逻辑放到CSV遍历循环中即可,完整修改后代码如下:

!pip install selenium
!pip install webdriver_manager
!pip install bs4

# 新增导入csv模块
import csv
from selenium import webdriver                             
from webdriver_manager.chrome import ChromeDriverManager  
from bs4 import BeautifulSoup
from selenium.webdriver.common.keys import Keys
from time import sleep

# ========== 以下逻辑仅执行一次 开始 ==========
driver = webdriver.Chrome(ChromeDriverManager().install())  
driver.get("https://newaccounttest.firstunited.test/bolts/")
sleep(2)

# 登录
user_id=driver.find_element_by_xpath("/html/body/div/div/ui-view/login/div/md-content/form/div/md-input-container[1]/input")
user_id.send_keys("sfreeman")
sleep(2)
password=driver.find_element_by_xpath("/html/body/div/div/ui-view/login/div/md-content/form/div/md-input-container[2]/input")
password.send_keys("Getmoney^7")
login_btn=driver.find_element_by_xpath("/html/body/div/div/ui-view/login/div/md-content/form/button").click()
sleep(3)

# 员工渠道
employee_channel=driver.find_element_by_xpath("/html/body/div/div/ui-view/desktop/blt-tiles/button[3]")
employee_channel.click()
sleep(10)
new_workspace=driver.find_element_by_xpath("/html/body/div/ui-view/ui-view/blt-tiles/button[2]")
new_workspace.click()
sleep(3)
# ========== 以上逻辑仅执行一次 结束 ==========

# 读取CSV文件
with open('applicants.csv', 'r', encoding='utf-8') as f:
    reader = csv.DictReader(f)
    # 遍历每个申请人
    for row in reader:
        print(f"正在处理申请人:{row['first_name']} {row['last_name']}")
        try:
            # 点击加号打开录入弹窗
            plus_btn=driver.find_element_by_xpath("/html/body/div/ui-view/ui-view/md-fab-speed-dial/md-fab-trigger/button").click()
            sleep(2)
            plus_btn=driver.find_element_by_xpath("/html/body/div/ui-view/ui-view/md-fab-speed-dial/md-fab-actions/div[1]").click()
            sleep(2)

            # 输入当前申请人的姓氏、名字
            last_name=driver.find_element_by_xpath("/html/body/div[3]/md-dialog/div[2]/div/form/div[1]/md-input-container[1]/input")
            last_name.clear() # 清空输入框避免残留
            last_name.send_keys(row['last_name'])
            sleep(1)
            first_name=driver.find_element_by_xpath("/html/body/div[3]/md-dialog/div[2]/div/form/div[1]/md-input-container[2]/input")
            first_name.clear()
            first_name.send_keys(row['first_name'])
            sleep(1)
            search_btn=driver.find_element_by_xpath("/html/body/div[3]/md-dialog/div[2]/div/form/div[2]/button")
            search_btn.click()
            sleep(2)
            
            # 输入邮箱
            emailfield=driver.find_element_by_xpath("/html/body/div[3]/md-dialog/div/md-tabs/md-tabs-content-wrapper/md-tab-content[1]/div/blt-tab/div/ng-transclude/ng-include/div/blt-tab-content/div/div/div/div[2]/ng-transclude/md-content/form/fieldset/md-whiteframe/ng-form/div/div/md-input-container[2]/input")
            sleep(3)
            emailfield.clear()
            emailfield.send_keys(row['email'])

            sleep(2)
            i_btn=driver.find_element_by_xpath("/html/body/div[3]/md-dialog/md-content/md-list/blt-search-item/md-list-item/div[2]/blt-search-toggle/button").click()
            sleep(2)
            addto_workplace=driver.find_element_by_xpath("/html/body/div[3]/md-dialog/md-content/md-list/blt-search-item/blt-search-detail/div/div/div/div/div[2]/button").click()
            sleep(2)
            
            # 可选:如果添加完成后弹窗未自动关闭,这里添加关闭弹窗的代码,示例:
            # close_btn = driver.find_element_by_xpath("你的关闭按钮XPath").click()
            # sleep(1)
            
            print(f"申请人{row['first_name']} {row['last_name']}处理完成")
        except Exception as e:
            print(f"处理申请人{row['first_name']} {row['last_name']}失败,错误信息:{str(e)}")
            # 可选:出错后强制关闭当前弹窗,避免影响下一次循环
            # driver.execute_script("document.querySelector('md-dialog').remove()")
            continue

# 所有申请人处理完成后自动退出
driver.quit()
print("所有申请人已处理完成,脚本退出")

注意事项

  • 可根据你的网站实际加载速度调整sleep()的时长,避免元素未加载完成导致报错
  • 如果页面XPath有变动,同步调整对应元素的定位路径即可
  • 若需要处理CSV编码问题,可将open()的encoding参数改为gbk适配Windows系统导出的CSV文件

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.19 02:54:08