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

如何用Selenium在无HTML链接的LinkedIn职位中打开新标签页并点击申请

问题:LinkedIn职位Apply按钮点击与新标签页打开实现

我正在编写一个Selenium脚本,目标是点击LinkedIn职位发布中的“Apply”按钮,让该职位在Chrome浏览器的新标签页中打开。但发现LinkedIn的HTML代码里完全没有包含该职位发布的链接,请问这种情况下我的目标仍可实现吗?

附上当前编写的代码:

driver.get('https://www.linkedin.com/jobs/search?keywords=software%20engineer&location=United%20States')

job_block = driver.find_element(By.CLASS_NAME, "scaffold-layout__list-container")
jobs = job_block.find_elements(By.TAG_NAME, "li")
n = len(jobs)
i = 0

while i < n:
    driver.implicitly_wait(0.5)
    jobs = job_block.find_elements(By.TAG_NAME, "li")
    n = len(jobs)
    i += 1
    # f.write(str(len(jobs)) + '\n')
    if i < n:
        ActionChains(driver).double_click(jobs[i]).perform()
        driver.implicitly_wait(0.5)
        driver.switch_to.window(driver.window_handles[-1])
        d1 = driver.find_element(By.CLASS_NAME, "jobs-search__job-details--wrapper")
        d2 = d1.find_element(By.CLASS_NAME, "job-view-layout.jobs-details")
        d3 = d2.find_element(By.CLASS_NAME, "t-14")
        d4 = d3.find_element(By.CLASS_NAME, "mt5")
        d5 = d4.find_element(By.CLASS_NAME, "display-flex")
        d6 = d5.find_element(By.CLASS_NAME, "jobs-s-apply.jobs-s-apply--fadein.inline-flex.mr2")
        driver.implicitly_wait(0.5)
        try:
            #f.write("trying\n")
            #d7 = d6.find_element(By.CLASS_NAME, "jobs-apply-button--top-card")
            #d7.find_element(By.XPATH, "//button").click()
            button = d6.find_element(By.CLASS_NAME, "jobs-apply-button.artdeco-button.artdeco-button--icon-right.artdeco-button--3 artdeco-button--primary.ember-view")
            button.click()
            f.write('button clicked\n')
        except:
            continue

解决方案

可以实现。LinkedIn的交互多由JavaScript触发,即便没有直接暴露跳转链接,也能通过模拟用户操作或捕获请求的方式达成需求,同时先修复你当前代码的定位问题:

1. 修复按钮定位错误

By.CLASS_NAME不支持同时传入多个类名(空格分隔代表多个独立类),改用CSS选择器定位按钮:

button = d6.find_element(By.CSS_SELECTOR, "button.jobs-apply-button.artdeco-button--primary")

同时建议用显式等待替代频繁设置的隐式等待,初始化driver时设置一次driver.implicitly_wait(10)即可,显式等待示例:

from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

# 等待按钮加载完成
button = WebDriverWait(driver, 10).until(
    EC.element_to_be_clickable((By.CSS_SELECTOR, "button.jobs-apply-button.artdeco-button--primary"))
)

2. 实现新标签页打开的两种方法

方法一:模拟Ctrl+点击(Windows/Linux)或Command+点击(Mac)

通过键盘快捷键模拟用户在新标签页打开链接的操作:

from selenium.webdriver.common.keys import Keys

# 找到按钮后,按下Ctrl并点击
ActionChains(driver).key_down(Keys.CONTROL).click(button).key_up(Keys.CONTROL).perform()

Mac系统替换为Keys.COMMAND即可。

方法二:捕获跳转URL后手动打开

利用Chrome DevTools协议监听网络请求,捕获Apply按钮触发的跳转链接,再用脚本打开新标签页:

# 开启网络请求监听
driver.execute_cdp_cmd('Network.enable', {})

def capture_request(request):
    if 'apply' in request['request']['url'] and request['type'] == 'Document':
        target_url = request['request']['url']
        driver.execute_script(f"window.open('{target_url}');")

# 绑定请求监听事件
driver.execute_cdp_cmd('Network.requestWillBeSent', {}, capture_request)

# 点击Apply按钮
button.click()

# 关闭监听
driver.execute_cdp_cmd('Network.disable', {})

注意事项

  • LinkedIn有严格的反爬机制,操作间隔不要过短,建议每次操作后添加time.sleep(2-3)避免触发限制;
  • 必须确保脚本运行时已登录LinkedIn,未登录状态下无法访问职位申请页面;
  • 若遇到验证码或账号验证,需手动处理或添加验证码识别逻辑。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.24 19:24:51