My goal is to close a Selenium session from an external Python script (not the same from where Selenium is actually running) and get a result as close as possible to the driver.quit()
webdriver method, but since it's not possible to call it from an external script, I am trying to kill all Selenium processes in the cleanest way.
There will be two files, one called test.py
which will run a Selenium session and another file called close_Selenium.py
from where the Selenium session will be closed.
Firstly, I wanted to know the processes that should be closed. After doing some research I found out that the code below using psutil
and subprocess
modules show the processes opened by Selenium (not sure if all of them, please let me know if you think there are some missing):
driver.service.process # is a Popen instance for the chromedriver process
p = psutil.Process(driver.service.process.pid)
print(p.children(recursive=True))
So, I added this at the end of my Selenium instance in the test.py
file:
import psutil
from subprocess import Popen
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
options = Options()
options.add_argument("-headless=new")
driver = webdriver.Chrome(options=options)
driver.get('https://stackoverflow.com/')
driver.service.process # is a Popen instance for the chromedriver process
p = psutil.Process(driver.service.process.pid)
print(p.children(recursive=True))
Running test.py
starts the Selenium session and also prints the pids which are opened by it:
[psutil.Process(pid=12740, name='conhost.exe', status='running', started='19:44:50'), psutil.Process(pid=12220, name='chrome.exe', status='running', started='19:44:50'), psutil.Process(pid=9588, name='chrome.exe', status='running', started='19:44:50'), psutil.Process(pid=2128, name='chrome.exe', status='running', started='19:44:50'), psutil.Process(pid=4112, name='chrome.exe', status='running', started='19:44:50'), psutil.Process(pid=11424, name='chrome.exe', status='running', started='19:44:50'), psutil.Process(pid=13064, name='chrome.exe', status='running', started='19:44:51'), psutil.Process(pid=6960, name='chrome.exe', status='running', started='19:44:51'), psutil.Process(pid=12864, name='chrome.exe', status='running', started='19:44:52')]
Now I know the pids that need to be closed, so I copy/paste every pid in the close_Selenium.py
file using the os module
and its os.kill
method for closing them. This is the close_Selenium.py
file:
import os
os.kill(int(12740), 2)
os.kill(int(12220), 2)
os.kill(int(9588), 2)
os.kill(int(2128), 2)
os.kill(int(4112), 2)
os.kill(int(11424), 2)
os.kill(int(13064), 2)
os.kill(int(6960), 2)
os.kill(int(12864), 2)
When running the code above works, the Selenium session is closed but it always throws an error: os.kill(int(12864), 2) OSError: [WinError 87] The parameter is incorrect
. It looks like the last os.kill(int(12864), 2)
line is the cause of this error.
This is all my work so far and my questions are:
Regarding my code:
Is there any way of getting all the above pids, save them in a file and then open it automatically from the
close_Selenium.py
script? At the moment I am doing that task manually and it´s very time consuming.Is it possible that that the last process is not necessary to be closed? Since it´s the cause of an arror in the
close_Selenium.py
script, it looks like that process can´t be found.
Regarding my goal in general:
- Is there any way more efficient and cleaner to close Selenium from an external Python script? Something similar to the
driver.quit()
method that could be called from an external script.
Pretty sure the answer to both questions are positive but since I am a Python and Selenium newbie, my knowlegde is not enough to find a way on my own.