Get PID of Browser launched by selenium

Harshavardhan Konakanchi picture Harshavardhan Konakanchi · May 25, 2012 · Viewed 25.6k times · Source

I would like to get the PID of the browser launched by selenium. Is there any way to get it done?

Answer

hwjp picture hwjp · Nov 30, 2012

Using the Python API, it's pretty simple:

from selenium import webdriver
browser = webdriver.Firefox()
print browser.binary.process.pid
# browser.binary.process is a Popen object...

If you're using Chrome, it's a little more complex, you go via a chromedriver process:

c = webdriver.Chrome()
c.service.process # is a Popen instance for the chromedriver process
import psutil
p = psutil.Process(c.service.process.pid)
print p.get_children(recursive=True)