9

I am running a Django service that fires up a chromedriver for selenium and scrapes a website for data. The Django service is called by another Java service through HTTP.

Here is the code:

views.py

path_to_chromedriver = '/path/to/chromedriver' 
browser = webdriver.Chrome(executable_path = path_to_chromedriver)
try:
    response = get_data(browser)
except Exception as e:
    print str(e)
finally:
    browser.close()
    browser.quit()

scraper.py

get_data(browser)
    try:
        .
        .
        .
        for i in range(1,6):
            try:
                .
                .
                .
             return "success data"
             except NoSuchElementException:
                 browser.back()
         raise Exception("No results found")
    except Exception as e:
         print str(e)
         raise

The problem is that after the java service has finished making all the calls and the whole process is complete, there are between 25 - 50 chrome processes orphaned in RAM occupying over 1 GB. Is there anything wrong I'm doing here?

Anirudh
  • 15,107
  • 2
  • 14
  • 26

3 Answers3

5

That's an old issue. What works for me, although dirty, is to add a sleep before quitting:

time.sleep(5)
browser.quit()
Ivan Chaer
  • 6,980
  • 1
  • 38
  • 48
0

Do not close before quit and surround code by try/except.

try:
    browser.quit()
except WebDriverException:
    pass
srjjio
  • 930
  • 1
  • 8
  • 16
0

Want to expand the answer given by @Serge B.

It's indeed better to use browser.quit() over browser.close() because the last one may close only your current window when the first one will close all windows (and all processes).

Still I don't think you'll solve your problem by doing this.

I had the same issue - a driver is not closed when it's run by a Java process. I encountered it while running my tests with TeamCity. Please try to run your code without the Java service you use and make sure it's the problem.

If it's the problem then I suggest to kill all the processes by Python methods. It's the solution I used and it worked for me. Unfortunately I used C# at that point and the next code helped me https://stackoverflow.com/a/35692319/4019586 but it's not what you can use now.

I don't know how to do it on Python but this may be a solution for you. I think with Python this will work https://stackoverflow.com/a/6278951/4019586.

So the positive side is that you do all correct!) It's just a bug in Selenium.

Community
  • 1
  • 1
Denis Koreyba
  • 3,144
  • 1
  • 31
  • 49