So I have a web scraper using selenium, code below. The problem is that each time the code is run, a new process seems to permanently appear as per htop- both a chromedriver process, and a XVFB process, as can be seen here https://i.stack.imgur.com/8wWql.png. I ran the function five times, and there are five XVFB's open (and 7 chromedrivers for some reason). I have a display.stop() and driver.close() which should prevent this happening? My code frequently throws an error but I have the stop/close instructions in the except as well so this should not affect the closing. I'm only running python 2.7 if that's relevant. The web scraper works fine other than this RAM clogging issue.
What is going on?
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import Select
from bs4 import BeautifulSoup
import time
import json
import traceback
from pyvirtualdisplay import Display
def scrapeBank(bank, return_dict):
try:
display = Display(visible=0, size=(800, 600))
display.start()
options = webdriver.ChromeOptions()
options.add_argument('--no-sandbox')
options.add_argument('--disable-extensions')
options.add_argument('--headless')
options.add_argument('--disable-gpu')
driver = webdriver.Chrome(chrome_options=options)
[do a bunch of stuff]
print('Bank Scrape completed')
display.stop()
driver.close()
return_dict['transactions'] = transactions
except:
display.stop()
driver.close()
print(traceback.format_exc())