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

为什么这段Python+Selenium实现的循环仅执行一次就停止?

问题原因及解决方法

  • 元素引用失效
    你预先在循环外定位的title、description、save、addarticle等页面元素,在第一次点击save或addarticle触发页面刷新/跳转后,旧的元素引用会直接失效。第二次循环操作过期元素时会抛出未找到元素的异常,没有异常捕获的情况下程序会直接终止,不会继续后续迭代。
    解决方法:将所有元素定位逻辑迁移到循环内部,每次迭代都重新获取当前页面的元素实例。
    调整后代码示例:

    import time
    
    for curr_row in range(1, sheet.nrows):
        titlevalue = sheet.cell_value(curr_row, 0)
        descriptionvalue = sheet.cell_value(curr_row, 2)
        imagevalue = sheet.cell_value(curr_row, 1)
        keywordsvalue = sheet.cell_value(curr_row, 3)
    
        # 每次循环重新定位元素,这里替换为你实际的元素定位方式和特征
        title = driver.find_element(By.ID, "title_input_id")
        description = driver.find_element(By.ID, "desc_input_id")
        image = driver.find_element(By.ID, "image_upload_id")
        keywords = driver.find_element(By.ID, "keywords_input_id")
        save = driver.find_element(By.ID, "save_btn_id")
        addarticle = driver.find_element(By.ID, "add_article_btn_id")
    
        title.send_keys(titlevalue)
        description.send_keys(descriptionvalue)
        image.clear()
        image.send_keys(imagevalue)
        keywords.send_keys(keywordsvalue)
        save.click()
        # 等待保存请求完成、页面状态更新
        time.sleep(2)
        addarticle.click()
        # 等待新增文章页加载完成
        time.sleep(2)
    
  • 页面未完成加载就执行下一步操作
    没有设置等待逻辑的情况下,点击save或addarticle后页面还在加载过程中,下一次循环操作的元素还未渲染完成,会直接抛出异常终止程序。
    解决方法:使用显式等待替代强制休眠,确保元素加载完成后再执行操作,稳定性更高:

    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.support import expected_conditions as EC
    from selenium.webdriver.common.by import By
    
    # 初始化最长等待时间为10秒
    wait = WebDriverWait(driver, 10)
    for curr_row in range(1, sheet.nrows):
        titlevalue = sheet.cell_value(curr_row, 0)
        descriptionvalue = sheet.cell_value(curr_row, 2)
        imagevalue = sheet.cell_value(curr_row, 1)
        keywordsvalue = sheet.cell_value(curr_row, 3)
    
        # 等待元素可交互后再定位
        title = wait.until(EC.element_to_be_clickable((By.ID, "title_input_id")))
        description = wait.until(EC.element_to_be_clickable((By.ID, "desc_input_id")))
        image = wait.until(EC.element_to_be_clickable((By.ID, "image_upload_id")))
        keywords = wait.until(EC.element_to_be_clickable((By.ID, "keywords_input_id")))
    
        title.send_keys(titlevalue)
        description.send_keys(descriptionvalue)
        image.clear()
        image.send_keys(imagevalue)
        keywords.send_keys(keywordsvalue)
        
        wait.until(EC.element_to_be_clickable((By.ID, "save_btn_id"))).click()
        # 等待新增按钮可点击后再执行点击
        add_article_btn = wait.until(EC.element_to_be_clickable((By.ID, "add_article_btn_id")))
        add_article_btn.click()
    
  • Excel读取逻辑异常
    可能存在JSON转Excel时格式错误、只有第一行是有效数据、xlrd识别行数错误的问题,导致循环本身就只执行了一次。
    解决方法:在循环开头添加打印逻辑验证循环执行次数:

    # 先打印总行数确认是否符合预期
    print("Excel有效总行数:", sheet.nrows)
    for curr_row in range(1, sheet.nrows):
        print(f"正在执行第{curr_row}次循环,当前行标题:{sheet.cell_value(curr_row, 0)}")
        # 后续原有逻辑
    

    如果控制台只输出一次循环日志,就需要检查你的Excel文件内容和xlrd读取代码是否正确。

  • 无异常捕获导致程序直接中断
    操作过程中可能出现弹窗遮挡、元素临时不可点击等偶发问题,没有异常捕获的情况下程序会直接停止运行。
    解决方法:给循环内逻辑添加异常捕获,打印错误信息的同时继续执行后续行的录入:

    for curr_row in range(1, sheet.nrows):
        try:
            # 原有循环内的所有代码
        except Exception as e:
            print(f"第{curr_row}行数据录入失败,错误信息:{str(e)}")
            # 可选:刷新页面回到新增文章页,避免影响后续循环
            driver.get("你的Django后台新增文章页面的直连地址")
    

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.09.27 13:45:08