Python Selenium:提取 Chrome 和 Firefox 浏览器的 PID

Python Selenium: Extracting the PID of Chrome and Firefox browser

如何获取Selenium 启动的Chrome/Chromium 或Firefox 浏览器的进程ID (PID)?我正在寻找一种在浏览器在 Selenium 网格中启动时也能正常工作的解决方案。

Selenium Grid 无法使用 driver.service.process.pid

启动带有 Selenium 的浏览器时,它会为配置文件设置创建一个新的临时目录(除非另有说明)。我们可以使用它来识别浏览器使用的特定进程:


In [470]: driver.capabilities
Out[470]: 
{'acceptInsecureCerts': False,
 'browserName': 'chrome',
 'browserVersion': '86.0.4240.198',
 'chrome': {'chromedriverVersion': '85.0.4183.87 (cd6713ebf92fa1cacc0f1a598df280093af0c5d7-refs/branch-heads/4183@{#1689})',
  'userDataDir': '/tmp/.com.google.Chrome.nW2W6p'},
 'goog:chromeOptions': {'debuggerAddress': 'localhost:44047'},
...}

此方法适用于 Firefox 和 Chrome,即使您有多个浏览器实例 运行ning,并且浏览器是通过 Selenium Grid 启动的(如果代码是 运行在节点服务器上)。

您需要安装 psutil:

pip install psutil
import psutil
import re
from typing import List


def pgrep(term, regex=False, full=True) -> List[psutil.Process]:
    """
    If `full`, then `term` is matched against the command line
    the process has been called with,
    else it is only matched against the process name.
    """
    procs = []
    for proc in psutil.process_iter(['pid', 'name', 'username', 'cmdline']):
        if full:
            name = ' '.join(proc.cmdline())
        else:
            name = proc.name()
        try:
            if regex and re.search(term, name):
                procs.append(proc)
            elif term in name:
                procs.append(proc)
        except psutil.NoSuchProcess:
            pass
    return procs


def browser_procs(driver) -> List[psutil.Process]:
    """
    Return the Processes associated with the browser
    (excluding geckodriver/chromedriver)
    """
    if driver.capabilities['browserName'] == 'firefox':
        directory = driver.capabilities['moz:profile']
    else:
        directory = driver.capabilities['chrome']['userDataDir']
    procs = pgrep(directory, full=True)
    procs.sort(key=lambda p: p.pid)
    return procs


def browser_proc(driver) -> psutil.Process:
    """
    Return the main Process of the browser
    (excluding geckodriver/chromedriver)
    """
    procs = browser_procs(driver)
    for proc in procs:
        name = proc.parent().name()
        if 'chromedriver' in name or 'geckodriver' in name:
            return proc
    raise ValueError



def driver_proc(driver) -> psutil.Process:
    """
    Return the Process of the geckodriver/chromedriver
    """
    return browser_proc(driver).parent()

让我们看看实际效果:

In [436]: driver = Chrome()

In [451]: browser_procs(driver)
Out[468]: 
[psutil.Process(pid=38453, name='chrome', status='sleeping', started='18:45:51'),
 psutil.Process(pid=38462, name='chrome', status='sleeping', started='18:45:51'),
 psutil.Process(pid=38463, name='chrome', status='sleeping', started='18:45:51'),
 psutil.Process(pid=38467, name='chrome', status='sleeping', started='18:45:51'),
 psutil.Process(pid=38486, name='chrome', status='sleeping', started='18:45:52'),
 psutil.Process(pid=38489, name='chrome', status='sleeping', started='18:45:52'),
 psutil.Process(pid=38521, name='chrome', status='sleeping', started='18:45:52'),
 psutil.Process(pid=38522, name='chrome', status='sleeping', started='18:45:52')]

In [471]: p = browser_proc(driver)
In [472]: p.pid
Out[472]: 38453

如果你想要chromedriver或者geckodriver的PID,那么你可以通过browser_proc(driver).parent()

得到那个进程

要检索 启动的 Chrome/Firefox 浏览器的进程 ID (PID),您可以使用以下解决方案:

火狐

  • 代码块:

    from selenium import webdriver
    
    driver = webdriver.Firefox(executable_path=r'C:\WebDrivers\geckodriver.exe')
    my_dict = driver.capabilities
    print("PID of the browser process is: " + str(my_dict['moz:processID']))
    
  • 控制台输出:

    PID of the browser process is: 2172
    

Chrome

  • 代码块:

    from selenium import webdriver
    from contextlib import suppress
    import psutil
    
    driver = webdriver.Chrome(executable_path=r'C:\WebDrivers\chromedriver.exe')
    driver.get('https://www.google.com/')
    for process in psutil.process_iter():
      if process.name() == 'chrome.exe' and '--test-type=webdriver' in process.cmdline():
              with suppress(psutil.NoSuchProcess):
                  print(process.pid)
    driver.quit()
    
  • 控制台输出:

    12384
    13656
    13800
    

参考资料

您可以在以下位置找到一些相关的详细讨论:

  • Get PID of Browser launched by selenium