user11940263
user11940263

Reputation:

Kill certain chrome process in python, not all

import subprocess
from selenium import webdriver
chrom_path = r"C:\Users\user\Downloads\chromedriver_win32\chromedriver.exe"
driver = webdriver.Chrome(chrom_path)
link = 'https://google.com'
driver.get(link)
s = driver.page_source
print((s.encode("utf-8")))
subprocess.call("TASKKILL /f  /IM  CHROME.EXE")
subprocess.call("TASKKILL /f  /IM  CHROMEDRIVER.EXE")

This code works good, but my code kills all chrome process not only this certain one. I want to kill only this chrome process that opens https://google.com

Is there any way to do that?

Upvotes: 5

Views: 10268

Answers (1)

undetected Selenium
undetected Selenium

Reputation: 193088

You are pretty correct.

  • subprocess.call("TASKKILL /f /IM CHROME.EXE"): This line of code will kill all the running chrome processes.
  • subprocess.call("TASKKILL /f /IM CHROMEDRIVER.EXE"): This line of code will kill all the running ChromeDriver processes.

You can find a detailed discussion in Selenium : How to stop geckodriver process impacting PC memory, without calling driver.quit()?


It would be difficult to identify and kill only this process that opens https://google.com that is being opened by the ChromeDriver because when automated tests are getting executed through Google Chrome you will observe that there are potentially dozens of Google Chrome processes running which can be observed through Windows Task Manager's Processes tab.

As per the article Why Google Chrome Has So Many Processes for a better user experience Google Chrome initiates a lot of background processes for each tab that have been opened by your Automated Tests. Google tries to keep the browser stable by separating each web page into as many processes as it deems fit to ensure that if one process fails on a page, that particular process(es) can be terminated or refreshed without needing to kill or refresh the entire page.

You can find a detailed discussion in Many process of Google Chrome (32 bit)

So, retrieving a single pid and killing the only process won't help you.


As per the best practices your test bed should be:

  • Configured with all the required softwares, libraries and binaries.
  • Test Execution must be performed in a controled environment for optimized performance.
  • While your @Tests are executing, it should be free from Manual Intervention.
  • Particularly when your @Tests are Selenium based, while test execution is InProgress the Test Environment shouldn't be intervened.

You can find a detailed discussion in Way to open Selenium browser not ovelapping my current browser

Upvotes: 2

Related Questions