Skip to content
Advertisement

Selenium leaves behind running processes?

When my selenium program crashes due to some error, it seems to leave behind running processes.

For example, here is my process list:

JavaScript

Here is my code:

JavaScript

Sometimes, the browser doesn’t load the webpage elements quickly enough so Selenium crashes when it tries to click on something it didn’t find. Other times it works fine.

This is a simple example for simplicity sake, but with a more complex selenium program, what is a guaranteed clean way of exiting and not leave behind running processes? It should cleanly exit on an unexpected crash and on a successful run.

Advertisement

Answer

Whats happening is that your code is throwing an exception, halting the python process from continuing on. As such, the close/quit methods never get called on the browser object, so the chromedrivers just hang out indefinitely.

You need to use a try/except block to ensure the close method is called every time, even when an exception is thrown. A very simplistic example is:

JavaScript

There are a number of much more sophisticated approaches you can take here, such as creating a context manager to use with the with statement, but its difficult to recommend one without having a better understanding of your codebase.

User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement