daOnlyBG
daOnlyBG

Reputation: 601

Why do Chrome.exe and chromedriver.exe still appear in the computer's memory, despite using driver.close() and driver.quit()?

The code I've written successfully does its part in crawling through a website with the appropriate date and time formatted in the URL, grabbing the table from the underlying HTML source code, and appending the results to a cache.

This python file gets run several dozen times (there are many agent IDs whose info I need to grab); after the script runs, however, dozens of chrome.exe and chromedriver.exe instances still appear in the computer's memory (this is visible in the computer's "Resource Monitor.")

Below is my code. I've used driver.quit() as well as driver.close() and even both together (with driver.close() coming first).

Isn't the driver.quit() supposed to close the instances in the computer's system? Why are they appearing in the memory? Is there a solution to this issue?

Please let me know if I can provide any further information. Thank you in advance.

from selenium import webdriver
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from datetime import datetime, timedelta
import credentials_page

def get_updated_url(agent_id_num):
    now = datetime.utcnow()
    today = datetime(now.year, now.month, now.day)
    yesterday = today - timedelta(days=1)
    previous_date_string = str(yesterday)[:10]
    return 'https://examplewebsite.com/agentId/'+agent_id_num+'/orderBy/ASC/startDate/'+previous_date_string+'%204:00%20AM/endDate/'+previous_date_string+'%2010:30%20PM'

def login_entry(username, password, browser): # logs into website
    login_email = browser.find_element_by_id('UserName')
    login_email.send_keys(username)
    login_password = browser.find_element_by_id('Password')
    login_password.send_keys(password)
    submit_elem = browser.find_element_by_xpath("//button[contains(text(), 'Log in')]")
    submit_elem.click()

def get_element(xpath, browser): # grabs element, turns it into needed table in raw HTML format
    table_of_interest = browser.find_element_by_xpath(xpath)
    # this has a type of <class 'selenium.webdriver.remote.webelement.WebElement'>
    return str('<table>'+table_of_interest.get_attribute('innerHTML')+'</table>')

def record_source_code(destination_cache, get_element_html): # takes element HTML and writes it to cache
    code_destination = open(destination_cache, 'w')
    code_destination.write(repr(get_element_html))
    code_destination.close()

def main_function(agent_id):
    driver = webdriver.Chrome()
    # figure out strings for start_date, end_date
    url = get_updated_url(agent_id)
    driver.get(url)
    #login
    login_entry(credentials_page.website_username, credentials_page.website_password, driver)
    # first test to see if "not found"
    if len(driver.find_elements_by_xpath("//*[text()='Not Found']"))>0:
        logoff_elem = driver.find_element_by_xpath("//*[contains(text(), 'Log off')]")
        logoff_elem.click()
        driver.quit()
        return False
    else:
        #grab table needed
        WebDriverWait(driver, 60).until(EC.presence_of_element_located((By.XPATH,'/html/body/div/div/div[2]/div[2]/table/tbody')))
        table_html = get_element('/html/body/div/div/div[2]/div[2]/table/tbody', driver)
        driver.quit()
        record_source_code('results_cache.html', table_html)
        return True

Upvotes: 3

Views: 444

Answers (1)

Buaban
Buaban

Reputation: 5137

I think the root cause is your code doesn't handle an exception. So when an exception occurs, it won't quit. Try/catch should help.

def main_function(agent_id):
    driver = webdriver.Chrome()
    # figure out strings for start_date, end_date
    url = get_updated_url(agent_id)
    try:
        driver.get(url)
        #login
        login_entry(credentials_page.website_username, credentials_page.website_password, driver)
        # first test to see if "not found"
        if len(driver.find_elements_by_xpath("//*[text()='Not Found']"))>0:
            logoff_elem = driver.find_element_by_xpath("//*[contains(text(), 'Log off')]")
            logoff_elem.click()
            driver.quit()
            return False
        else:
            #grab table needed
            WebDriverWait(driver, 60).until(EC.presence_of_element_located((By.XPATH,'/html/body/div/div/div[2]/div[2]/table/tbody')))
            table_html = get_element('/html/body/div/div/div[2]/div[2]/table/tbody', driver)
            driver.quit()
            record_source_code('results_cache.html', table_html)
            return True
    except:
        drier.quit() #<-- Try/catch and close it

Upvotes: 1

Related Questions