When launching a browser with Selenium, it creates a new temporary directory for the profile settings (unless otherwise specified). We can use this to identify the specific process(es) that the browser uses:
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'},
...}
This approach works for both Firefox and Chrome, even if you have multiple instances of a browser running, and also if the browsers are launched through Selenium Grid (if the code is run on the node server).
You will need to install 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()
Let's see it in action:
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
If you want the PID of the chromedriver or geckodriver, then you can get that process through browser_proc(driver).parent()