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

Selenium+Pandas批量创建网站用户:循环迭代CSV行求助

解决Selenium+Pandas批量注册用户的循环迭代问题

问题说明

现有代码可读取CSV并成功创建第一行数据对应的用户,但无法自动遍历所有行完成批量注册;手动运行第二行数据也能成功,需实现循环遍历所有有效行(直至空单元格)的逻辑。

修改后的完整代码

import pandas as pd
import time
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

# 读取CSV并过滤全空行
df = pd.read_csv('testfile.csv').dropna(how='all')

# 初始化浏览器并完成登录
driver = webdriver.Chrome()
driver.get('xxxxxxx')
driver.find_element(By.ID, "ctl00_ctl00_phM_phB_Username").send_keys('xxxxx')
driver.find_element(By.ID, "ctl00_ctl00_phM_phB_Password").send_keys('xxxxxx')
driver.find_element(By.ID, "ctl00_ctl00_phM_phB_LoginButton").click()
driver.find_element(By.LINK_TEXT, "Administration").click()

# 遍历每一行数据创建用户
for index, row in df.iterrows():
    # 遇到全空行则终止循环
    if row.isnull().all():
        break
    
    # 等待并点击创建新用户按钮
    create_btn = WebDriverWait(driver, 10).until(
        EC.element_to_be_clickable((By.ID, "ctl00_ctl00_phM_phB_UserAdministrationUserControl_CreateNewUserButton"))
    )
    create_btn.click()
    
    # 填写用户信息(用当前行数据替代原固定第一行)
    WebDriverWait(driver, 10).until(
        EC.visibility_of_element_located((By.ID, "ctl00_ctl00_phM_phB_txtFirstName"))
    ).send_keys(row['firstname'])
    driver.find_element(By.ID, "ctl00_ctl00_phM_phB_txtLastName").send_keys(row['lastname'])
    driver.find_element(By.ID, "ctl00_ctl00_phM_phB_txtEmail").send_keys(row['username'])
    driver.find_element(By.ID, "ctl00_ctl00_phM_phB_txtNotificationEmail").send_keys(row['email'])
    driver.find_element(By.ID, "ctl00_ctl00_phM_phB_txtVerifyNotificationEmail").send_keys(row['email'])
    driver.find_element(By.ID, "ctl00_ctl00_phM_phB_txtPhone").send_keys(row['phone'])
    
    # 完成用户信息填写后的流程
    driver.find_element(By.ID, "btnNext").click()
    time.sleep(1)
    
    driver.find_element(By.ID, "chkOrderProducts").click()
    time.sleep(1)
    
    acct_list = driver.find_element(By.ID, "ctl00_ctl00_phM_phB_AccountNumbersList1_lstAvailableAcctNumbers")
    acct_list.click()
    time.sleep(1)
    acct_list.send_keys(Keys.ARROW_UP)
    acct_list.send_keys(Keys.TAB)
    driver.find_element(By.ID, "ctl00_ctl00_phM_phB_AccountNumbersList1_btnAddAccount").send_keys(Keys.SPACE)
    
    # 根据网站实际流程,添加返回管理页面的操作(如果需要)
    # driver.find_element(By.LINK_TEXT, "返回用户管理").click()
    time.sleep(2)

# 批量创建完成后关闭浏览器
driver.quit()

关键修改说明

  • 循环遍历:用df.iterrows()遍历每一行数据,替换原代码中仅取第一行([0])的固定逻辑。
  • 空行处理:先通过dropna(how='all')过滤CSV中的全空行,循环内再添加判断,遇到全空行直接终止。
  • 稳定性优化:用WebDriverWait显式等待替代部分time.sleep,确保元素加载完成后再操作,避免页面加载延迟导致的报错。
  • 流程复用:登录与进入管理页面仅执行一次,循环内重复执行「创建新用户→填写信息→完成配置」的核心流程。

额外提示

  • 如果网站创建用户后未自动返回管理页面,需补充代码跳转回用户管理界面,保证下一次循环能正常点击创建按钮。
  • 可添加try-except块捕获异常,避免某一行数据出错导致整个批量流程中断。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.15 21:46:01