Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get PID of Browser launched by selenium

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

like image 501
Harshavardhan Konakanchi Avatar asked May 25 '12 10:05

Harshavardhan Konakanchi


People also ask

How do I find my browser PID?

The location of the geckodriver.exe file is passed as a parameter to that class. This is done by setting the path to executable_path property. Then, the browser shall be launched with the get method. Finally, to obtain the PID of the browser, we shall use the driver.service.process.id method.

How do I find the PID of Chrome?

Chrome has a built in task manager that will show you PID. You can use Shift + ESC keyboard combination to access or you could go to Options -> More Tools -> Task Manager. You may have to increase the width of the window to see it.


2 Answers

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) 
like image 184
hwjp Avatar answered Sep 18 '22 14:09

hwjp


hwjp's solution isn't working anymore for me, but the solution from ABM is working for other browsers too in case anyone is wondering, so for firefox as of now:

from selenium import webdriver driver = webdriver.Firefox() print(driver.service.process.pid) 

can't comment because of reputation, so I'm submitting this as separate answer...

like image 27
flashback Avatar answered Sep 20 '22 14:09

flashback