Python Selenium脚本批量上传Redbubble仅重复读取CSV首行问题求助
问题原因
脚本重复上传第一行数据的核心问题有两点:
- 字段取值逻辑写在了
for循环外部,提前固定line_number = 1把图片路径、标题、标签、描述、色值全部写死成了CSV第一行数据(CSV索引从0开始,0是表头行,1对应第一条数据),循环执行时全程没有读取当前遍历行的内容。 - 循环遍历范围包含了CSV表头行,会把表头字段当成一条作品数据尝试上传。
修复方案
把字段取值逻辑移到循环内部,遍历的时候跳过表头行即可,修改后的完整代码段如下:
import csv import time from selenium.webdriver.common.by import By filename = 'img.csv' with open(filename, 'r') as f: mycsv = list(csv.reader(f)) # 从索引1开始遍历,直接跳过第0行的表头 for line in mycsv[1:]: # 每次循环读取当前行的对应字段 images = line[0] titels = line[1] tags = line[2] descriptions = line[3] colors = line[4] driver.get('https://www.redbubble.com/portfolio/images/') time.sleep(3) # 页面滚动到操作区域 driver.execute_script("window.scrollTo(0,400)") # 点击背景色选择控件 color_trigger = driver.find_element(By.XPATH, "//div[@class='global-background-color-setting']//div[@class='sp-replacer sp-light']") color_trigger.click() # 输入背景色值 color_input = driver.find_element(By.XPATH, "//div[@class='sp-container sp-light sp-buttons-disabled sp-palette-buttons-disabled sp-palette-disabled']//input[@type='text']") color_input.click() color_input.clear() color_input.send_keys(colors) time.sleep(2) # 输入作品标题 title_input = driver.find_element(By.XPATH, "//input[@id='work_title_en']") title_input.click() title_input.clear() title_input.send_keys(titels) time.sleep(1) # 输入作品标签 tag_input = driver.find_element(By.XPATH, "//textarea[@id='work_tag_field_en']") tag_input.click() tag_input.clear() tag_input.send_keys(tags) time.sleep(2) # 输入作品描述 desc_input = driver.find_element(By.XPATH, "//textarea[@id='work_description_en']") desc_input.click() desc_input.clear() desc_input.send_keys(descriptions) time.sleep(2) # 上传对应设计图 upload_input = driver.find_element(By.XPATH, "//input[@id='select-image-base']") time.sleep(2) upload_input.send_keys(images) # 此处补全你原有脚本里的发布提交、等待上传完成的逻辑 time.sleep(10) # 预留足够加载时间,避免页面未完成操作就跳转下一轮循环
注意事项
- 确认
img.csv中存储的图片路径为本地绝对路径,否则Selenium无法定位到文件会导致上传失败。 - 原代码存在重复使用同名变量存储不同元素的问题,修复版做了变量名区分,避免变量覆盖引发的元素定位报错。
- 可以根据自己的网络速度调整各步骤的等待时长,减少操作超时的概率。
内容的提问来源于stack exchange,提问作者Nouari
相关产品推荐
相关产品推荐

