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

如何用Python Selenium循环实现招聘工具候选人简历批量下载?

批量下载招聘工具收件箱中所有候选人简历的循环实现

问题背景

现有一款招聘工具,收件箱内包含多位候选人(如HR、RJ、IE),候选人姓名为蓝色超链接,点击可进入详情页下载简历。目前已实现首位候选人的简历下载逻辑,但需要修改为循环处理所有候选人的批量操作。

原单候选人处理代码如下:

# click on the candidate name ('Hane')...
driver.find_element(By.XPATH,'//*[@id="cdk-drop-list-20"]/div[1]/orgos-recruiting-kanban-card/orgos-container/orgos-column-container/orgos-column/orgos-column-container/orgos-column[2]/kenjo-truncate-text[1]/div[2]/div/span/a').click()

# inside the candidate page, find the element with 3 dots and click on it so that the download button becomes visible
WebDriverWait(driver, 20).until(
EC.element_to_be_clickable((By.XPATH, '/html/body/orgos-web-app/orgos-cloud-shell/div/div/div/div/div/orgos-recruiting-candidate-main/div/div/div/orgos-column-container/orgos-column[2]/orgos-recruiting-candidate-attachments/orgos-container/orgos-table/div/div/div[1]/div/table/tbody/tr[1]/td[4]/kenjo-icon/mat-icon'
                           ))).click()

# click on the download button
WebDriverWait(driver, 20).until(
EC.element_to_be_clickable((By.XPATH, 
    '//*[@id="mat-menu-panel-10"]/div/button[3]'))).click()

# Go back to the main inbox
driver.back()

收件箱容器的HTML类信息:

"_ngcontent-rop-c418 ckdroplist
cdkdroplistsortingdisabled class="cdk-drop-list
rpk-dropping-area"

解决方案

核心思路

  1. 替换动态定位符:原代码使用的cdk-drop-list-20、mat-menu-panel-10为动态生成ID,会随页面刷新变化,需改为相对定位保证稳定性。
  2. 循环遍历候选人:先定位收件箱容器,获取所有候选人姓名链接,循环处理每个候选人的简历下载流程。
  3. 避免元素失效:每次返回收件箱后,重新获取候选人元素列表(或通过索引定位),防止StaleElementReferenceException。

优化后的批量处理代码

from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
import time

# 等待收件箱加载完成
inbox_container = WebDriverWait(driver, 20).until(
    EC.presence_of_element_located((By.CSS_SELECTOR, ".cdk-drop-list.rpk-dropping-area"))
)

# 获取候选人数量(用于循环)
candidate_count = len(inbox_container.find_elements(By.CSS_SELECTOR, "orgos-recruiting-kanban-card"))

# 循环处理每个候选人
for i in range(candidate_count):
    # 每次循环重新定位收件箱和当前候选人(避免元素失效)
    inbox_container = WebDriverWait(driver, 20).until(
        EC.presence_of_element_located((By.CSS_SELECTOR, ".cdk-drop-list.rpk-dropping-area"))
    )
    # 定位第i+1位候选人的姓名链接(索引从0开始)
    candidate_link = inbox_container.find_elements(By.CSS_SELECTOR, "orgos-recruiting-kanban-card kenjo-truncate-text[1] div[2] div span a")[i]
    candidate_link.click()
    
    # 等待详情页加载,点击更多按钮(三个点)
    WebDriverWait(driver, 20).until(
        EC.element_to_be_clickable((By.CSS_SELECTOR, "orgos-recruiting-candidate-attachments orgos-table tbody tr td[4] kenjo-icon mat-icon"))
    ).click()
    
    # 点击下载按钮(使用相对定位,避免动态ID)
    WebDriverWait(driver, 20).until(
        EC.element_to_be_clickable((By.CSS_SELECTOR, "mat-menu-panel button:nth-child(3)"))
    ).click()
    
    # 等待下载触发,返回收件箱
    time.sleep(1)  # 根据实际下载速度调整等待时间
    driver.back()
    # 等待收件箱重新加载
    WebDriverWait(driver, 20).until(
        EC.presence_of_element_located((By.CSS_SELECTOR, ".cdk-drop-list.rpk-dropping-area"))
    )

关键优化点

  • 定位稳定性:使用CSS选择器替代带动态ID的XPATH,如.cdk-drop-list.rpk-dropping-area定位收件箱容器,orgos-recruiting-kanban-card定位候选人卡片。
  • 循环可靠性:每次循环重新获取元素,避免返回收件箱后原元素引用失效。
  • 兼容性:用nth-child(3)定位下载按钮,替代动态生成的mat-menu-panel-10ID。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.31 01:39:40