【问题标题】:Is there a way to whitelist chromedriver process name while killing other processes?有没有办法在杀死其他进程的同时将 chromedriver 进程名称列入白名单?
【发布时间】:2026-02-04 02:10:02
【问题描述】:

首先,我想说这是一个个人脚本,我是唯一一个会运行它的人。

我正在编写一个生产力程序,该程序会阻止应用程序在特定时间运行,chrome 就是其中之一。但是,我每天都会进行大量网络抓取,因此我需要在代码运行时将 chromedriver 的关闭“列入白名单”。

这就是我的使用方式:

import subprocess
imgNames = ["Chrome.exe"] # There are several processes on this list, not only chrome.
for img in imgNames:
     subprocess.call(['taskkill', '/F', '/IM', img], shell=True)

由于 chromedriver 具有相同的 .exe 名称,我在想也许有一种方法可以更改进程名称或其他识别它的方法,因此它在块会话期间保持打开状态。

【问题讨论】:

  • 您可能希望在标题中指定这是一个仅限 Windows 的问题。如果您尝试编写可在其他平台上运行的东西,那将有一个非常不同的答案(您需要放弃 taskkill 整体使用;我通常建议用 Python psutil 模块替换它)。
  • ChromeDriver 没有相同的进程名称...它是 chromedriver.exe 而不是 chrome.exe
  • 这不太正确。每当我运行 selenium 脚本时,都会有两个进程(chrome.exe 和 chromedriver.exe),但是,我的代码会终止 chrome.exe 任务并用它关闭我的 chromedriver 浏览器。

标签: python selenium selenium-webdriver selenium-chromedriver


【解决方案1】:

您可以采用以下策略从整个 total_processes 列表中排除 whilelist_process 列表:

whilelist_process = "chromedriver.exe"
total_processes = ["Chrome.exe", ...] # There are several processes on this list, not only chrome.
kill_processes = [x for x in total_processes if x != whilelist_process]
for proc in kill_processes:
     subprocess.call(['taskkill', '/F', '/IM', proc], shell=True)

【讨论】:

    【解决方案2】:

    对于将来面临同样问题的任何人:这可能不是您所期望的答案,但我最终使用了 Firefox 的 geckodriver 而不是 chromedriver,现在我可以杀死 chromedriver 并且仍然打开 selenium 浏览器其余的被阻止。

    【讨论】:

      最近更新 更多