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

修复Indeed职位申请自动化Python脚本:Safari无交互自动退出问题

Indeed职位申请自动化脚本修复方案

问题描述

想要实现Indeed平台职位申请流程自动化,先完成自动打开每个职位招聘页面新标签的功能(后续手动填写申请信息)。但运行脚本后,Safari浏览器启动不久就自动退出,未执行任何页面交互操作,推测是页面上的“接受Cookie”弹窗阻碍了后续操作。

原代码如下:

#!/usr/bin/env python3
from selenium import webdriver
import time

class IndeedSpider:
    def __init__(self):
        self.driver = webdriver.Safari()

    def apply_to_jobs(self):
        self.driver.get('https://emplois.be.indeed.com/jobs?q=job+étudiant&l=Bruxelles&from=searchOnHP&vjk=8038d2843bbd3e80')
        self.driver.implicitly_wait(10)  # Wait for the page to load

        apply_now_buttons = self.driver.find_elements("xpath", '//button[@aria-label="Postuler maintenant opens in a new tab"]')[:5]
        for button in apply_now_buttons:
            button.click()
            time.sleep(2)  # Wait for the page to load after clicking

# Instantiate the spider and apply to jobs
spider = IndeedSpider()
spider.apply_to_jobs()

问题分析

  1. Cookie弹窗会覆盖页面元素,导致Selenium无法定位到目标按钮,脚本因找不到元素直接结束运行
  2. 隐式等待仅能等待页面加载,无法处理动态弹出的Cookie授权窗口
  3. 未添加异常处理,单个操作失败会导致整个脚本终止

修复后的代码

#!/usr/bin/env python3
from selenium import webdriver
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

class IndeedSpider:
    def __init__(self):
        self.driver = webdriver.Safari()
        self.wait = WebDriverWait(self.driver, 15)  # 显式等待最长15秒

    def handle_cookie_popup(self):
        try:
            # 等待并点击接受Cookie按钮(根据页面实际元素调整)
            accept_cookie_btn = self.wait.until(
                EC.element_to_be_clickable((By.XPATH, '//button[contains(text(), "Accepter")]'))
            )
            accept_cookie_btn.click()
            time.sleep(1)
        except Exception as e:
            print("未检测到Cookie弹窗,或弹窗处理失败:", e)

    def apply_to_jobs(self):
        self.driver.get('https://emplois.be.indeed.com/jobs?q=job+étudiant&l=Bruxelles&from=searchOnHP&vjk=8038d2843bbd3e80')
        
        # 先处理Cookie弹窗
        self.handle_cookie_popup()

        # 等待申请按钮加载完成
        apply_now_buttons = self.wait.until(
            EC.presence_of_all_elements_located((By.XPATH, '//button[@aria-label="Postuler maintenant opens in a new tab"]'))
        )[:5]

        for idx, button in enumerate(apply_now_buttons):
            try:
                # 确保按钮可点击
                self.wait.until(EC.element_to_be_clickable(button)).click()
                print(f"已打开第{idx+1}个职位页面")
                time.sleep(2)
            except Exception as e:
                print(f"点击第{idx+1}个按钮失败:", e)
                continue

# 执行脚本
if __name__ == "__main__":
    spider = IndeedSpider()
    try:
        spider.apply_to_jobs()
    finally:
        # 可选:保持浏览器打开,方便后续手动操作;若需自动关闭可注释掉下面的input
        input("完成后按回车关闭浏览器...")
        spider.driver.quit()

关键优化点

  • 显式等待替代隐式等待:使用WebDriverWait精准等待元素加载,比固定时长的time.sleep和隐式等待更可靠
  • Cookie弹窗处理:优先处理Cookie授权,避免弹窗遮挡目标元素
  • 异常处理:每个操作添加异常捕获,防止单个按钮点击失败导致脚本中断
  • 手动关闭控制:添加input让浏览器保持打开,方便后续手动填写申请信息

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.24 15:44:51