1

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.

David Miller
  • 512
  • 2
  • 4
  • 17
  • 1
    Your code trials? – undetected Selenium Sep 25 '18 at 06:46
  • @Newcontributor: What is a code trial? FYI I am a newbie and in over my head... – David Miller Sep 25 '18 at 20:23
  • 1
    The code block you are trying to execute – undetected Selenium Sep 25 '18 at 20:36
  • @Newcontributor: Thanks for taking a look. I just modified the original post to include a code snippet where it crashes. – David Miller Sep 25 '18 at 23:46
  • @Newcontributor: Also updated to include the code where the Lost UI shared context warning occurs, which is right when the selenium chrome driver is instantiated. I've played w/many chrome options but maybe a setting is off... I've tried changing settings w/no luck yet... – David Miller Sep 26 '18 at 18:04
  • 1
    Not concerned with the error log `Lost UI shared context` but still trying to analyze `PermissionError: [WinError 32] The process cannot access the file because it is being used by another process`. – undetected Selenium Sep 26 '18 at 18:31
  • 1
    @Newcontributor: You rock thanks for looking let me know if there's anything else I can provide but I think that's all I got. – David Miller Sep 26 '18 at 20:59
  • 1
    Can you update the question with the exact webdriver initialization code and the `tearDown()` method where you destroy the webdriver & web-browser instance? – undetected Selenium Sep 26 '18 at 21:02
  • @Newcontributor: I modified my comments above. The web driver initialization code is exactly where the Lost UI shared context warning occurs. And I added the method used to tear down the selenium driver after use. – David Miller Sep 28 '18 at 00:46
  • 1
    @Newcontributor: Thank you so much for your feedback. It turns out that I was not properly doing teardown of orphaned selenium drivers, so your comments helped me resolve this issue. Once I make sure to kill orphaned drivers there is no permission error. All fixed now. – David Miller Sep 28 '18 at 14:37
  • 1
    Glad to be able to help you out. Incase you want to know more why I wasn't concerned about **Lost UI shared context** watch [this](https://stackoverflow.com/questions/50143413/errorgpu-process-transport-factory-cc1007-lost-ui-shared-context-while-ini/50148698#50148698) discussion. – undetected Selenium Sep 28 '18 at 14:39
  • 1
    @Newcontributor: Cool, thanks – David Miller Sep 28 '18 at 15:10

0 Answers0