I am building a Tkinter app with python that initializes multiple selenium webdrivers. The initial problem was that lots of chromedriver.exe instances were filling up user's memory, even after using driver.quit()
(sometimes). So to get rid of this issue, when closing the tkinter app, I wrote this line os.system("taskkill /f /im chromedriver.exe /T")
, that solves my problem, but, by using this, a command prompt instance is initiated that self kills almost instantly. The problem is that the user can see it and I find it kinda disturbing. Is there any way I could hide it? Or is there a workaround for my initial problem, that is user friendly?
Asked
Active
Viewed 148 times
-2

Theodore
- 21
- 4
-
1Does this answer your question? [Running windows shell commands with python](https://stackoverflow.com/questions/14894993/running-windows-shell-commands-with-python) – Greg Burghardt Apr 24 '20 at 00:06
2 Answers
0
Use both driver.close() and driver.quit() in your code in order to free memory.
driver.close()
driver.quit()

Deadmeat
- 29
- 3
0
To reduce the memory footprint you should use ChromeDriver-Service:
First you start the service, then use it when creating new drivers, and finally stop the service before program exit.
Put the code below in chrome_factory.py and then:
- on program start call chrome_factory.start_service()
- to create new driver call chrome_factory.create_driver()
- the service and drivers will be automatically stopped/quit at program exit.
Using this approach will result in only ever having single chromedriver.exe process.
# chrome_factory.py
import atexit
from os.path import expanduser
from typing import Optional
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
EXECUTABLE = expanduser("~/bin/chromedriver")
_chrome_service: Optional[Service] = None
def start_service():
global _chrome_service
_chrome_service = Service(EXECUTABLE)
_chrome_service.start()
atexit.register(_chrome_service.stop)
def create_driver() -> webdriver.Remote:
global _chrome_service
opts = webdriver.ChromeOptions()
opts.add_argument("--headless")
driver = webdriver.Remote(_chrome_service.service_url,
desired_capabilities=opts.to_capabilities())
atexit.register(driver.quit)
return driver
def main():
start_service()
for _ in range(20):
create_driver()
if __name__ == '__main__':
main()

Hrisimir Dakov
- 557
- 3
- 9
-
This is really helpful. But that single chromedriver.exe process still remains in task manager memory, right? If so, how can I get rid of that too? I am asking this because I am facing an issue with `ChromeDriverManager().install()`, that raises `PermissionError`, meaning all chromedriver processes need to be killed. – Theodore Apr 25 '20 at 16:56