I'm getting a permission error when I try to delete a file, but only if the python script has tripped a Lost UI shared context warning previously.
Here are the error messages:
ERROR:gpu_process_transport_factory.cc(1016)] Lost UI shared context.
PermissionError: [WinError 32] The process cannot access the file
because it is being used by another process
Interestingly, when the Lost UI shared context warning message occurs, selenium seems to automatically re instantiate chrome windows - when I run it in foreground a new window is created for each Lost UI shared context warning, and the orphaned windows persist. I am not explicitly creating the new chrome windows, some other process is, presumably selenium or chrome driver.
I am guessing that somehow the file I cannot delete is attached to one of the orphaned processes, but I can't seem to figure out how to disconnect it so I can delete it, which I need to do for my code to function.
Solving the Lost UI shared context warning is not important to me, only inasmuch as it may be the cause of the permission error.
Here is a where the code crashes with PermissionError at os.remove(f)
:
# downloading file - first clear download folder
clearfolder(downloadfolder)
xpath = "xpath-of-the-file-i-want-to-download"
button = getbutton(self.driver, xpath)
# make sure element is visible
self.driver.execute_script("return arguments[0].scrollIntoView(true);", button)
time.sleep(2)
button.click()
time.sleep(2)
# get name of downloaded file - there should only be one file in the download folder
files = []
for (dirpath, dirnames, filenames) in os.walk(self.downloadfolder):
files.extend(filenames)
break
self.logger.debug('Files in directory:\n{}'.format(files))
if len(files) != 1:
self.logger.warning('Single file condition not satisfied with {} files present'.format(len(files)))
return 1, []
else:
self.logger.debug('Reading excel file')
f = os.path.join(self.downloadfolder, files[0])
excelfile = pd.ExcelFile(f)
df = excelfile.parse(header=[0,1],index_col=None)
excelfile.close()
self.logger.debug('Deleting excel file')
time.sleep(1)
# I tried to see if chmod could change the permissions so I could delete the file - it did not affect the error
self.logger.debug('chmod')
os.chmod(f, stat.S_IWRITE)
time.sleep(1)
os.remove(f)
self.logger.debug('Done deleting excel file')
And here is where the selenium chrome driver is created, which coincides with where the Lost UI shared context warning occurs, apparently right when the driver is defined - driver = webdriver.Chrome
:
capabilities = DesiredCapabilities.CHROME.copy()
# this lets get load page without waiting
capabilities['pageLoadStrategy'] = 'none'
options = webdriver.ChromeOptions()
options.add_argument('--start-maximized')
options.add_argument('--no-proxy-server')
options.add_argument('--disable-extensions')
#options.add_argument('--disable-gpu')
options.add_argument('--dns-prefetch-disable')
options.add_argument('--lang=en_US')
options.add_argument('--disable-popup-blocking')
prefs = {"profile.default_content_settings.popups": 0,
"platform": "WINDOWS",
"download.default_directory": downloadfolder,
"directory_upgrade": True,
"extensions_to_open": ""}
options.add_experimental_option("prefs", prefs)
driver = webdriver.Chrome(executable_path=driverloc, chrome_options=options, desired_capabilities=capabilities)
# not sure why but chrome sometimes takes a long time to load pages
driver.set_page_load_timeout(300)
And this is the method which is used to clean up each selenium chrome driver once it has done its job:
def killdriver(driver):
"""Kill driver. Handles the case where driver has not been instantiated"""
try:
driver
except NameError as e:
logger.warning('Kill driver - NameError {}'.format(e))
pass
except AttributeError as e:
logger.warning('Kill driver AttributeError {}'.format(e))
pass
except OSError as ConnectionRefusedError:
logger.warning('Kill driver ConnectionRefusedError')
else:
# driver exists so kill it
logger.info('Killing driver')
driver.quit()
Thanks for any help.