'Selenium Python - loop gets slower everytime

Im trying to automize downloading files, from a list of "cars" then save the file on my computer unzip it and rename it with the name of the car.

Problem is, my forloop is getting increasingly slower. The first loop takes about 1 second to execute, but the 10th loop takes almost a minute.

Could someone review my code and point out potential flaws? i'm using chromedriver.

for car in carList:
                try:
                    #Apre il filter
                    filter = WebDriverWait(driver, 10).until(
                        EC.element_to_be_clickable((By.XPATH, '//*[contains(text(), "filter")]'))
                    )    
                    filter.click()
    
                    #Find car
                    n_sport = WebDriverWait(driver, 10).until(
                        EC.presence_of_element_located((By.CSS_SELECTOR, '#door'))
                    )
                    n_sport.send_keys(car)
                    n_sport.send_keys(Keys.ENTER)
                    
                    #open_car = driver.find_element_by_xpath("//a[contains(@title," + "0" +car +")]")
                    open_car = WebDriverWait(driver, 30).until(
                        EC.element_to_be_clickable((By.XPATH, "//a[contains(@title," + "0" +car +")]")
                    ))
                    open_car.click()
                    
                    ##driver.execute_script("arguments[0].click();", checkbox_elem)                
                    
                    #open file system
                    fs = WebDriverWait(driver, 30).until(
                      EC.element_to_be_clickable((By.XPATH, '//*[contains(text(), "file system")]'))
                    )
                    fs.click()
    
    
                    #open C:
    
                    c = WebDriverWait(driver, 60).until(
                      EC.element_to_be_clickable((By.XPATH, '//*[contains(text(), "C:")]'))
                    )
                    c.click()
    
    
                    #Right click on file:
    
                    f = WebDriverWait(driver, 60).until(
                      EC.element_to_be_clickable((By.XPATH, textToSearch))
                    )
        
                    ActionChains(driver).context_click(f).perform()
    
    
    
                    #Save
    
                    save = WebDriverWait(driver, 10).until(
                      EC.element_to_be_clickable((By.XPATH, '//*[contains(@href, "#GETFILE")]'))
                    )
        
                    save.click()
    
                    #Find empty button:
                    empty = WebDriverWait(driver, 10).until(
                        EC.element_to_be_clickable((By.XPATH, '//*[contains(text(), "Svuota elenco")]'))
                    )
    
                    #Empty
                    empty.click()
                    
                    print('The file has been downloaded from the car: ' + car )
    
                    if doesZipExists:
                        renameFile(parentFolder, ext, car, fname)
                    
                except:
                    print('Error: car ' + car + ' has not been found')


Solution 1:[1]

It may be that you have stuff left in memory that is not getting cleared, you could always manually clear out the memory in a finally block:

for car in carList:
            try:
                #Apre il filter
                filter = WebDriverWait(driver, 10).until(
                    EC.element_to_be_clickable((By.XPATH, '//*[contains(text(), "filter")]'))
                )    
                ...
                if doesZipExists:
                    renameFile(parentFolder, ext, car, fname)
                
            except:
                print('Error: car ' + car + ' has not been found')
            finally:
                del filter,
                del n_sport, 
                del open_car,
                del fs, 
                del c, 
                del f, 
                del save, 
                del empty

I would also avoid using python keywords (e.g filter) as variable names as you can fall into all sorts of issues there.

There is one other factor that I can't see, the renameFile(parentFolder, ext, car, fname) call you make, so this could potentially be causing you trouble too, especially considering this is looks like an IO operation.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Leroy