尝试删除文件时出现权限错误,但前提是python脚本之前触发了“丢失的UI共享上下文”警告。
以下是错误消息:
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
有趣的是,当出现“丢失的UI共享上下文警告”消息时,硒似乎会自动重新实例化chrome窗口-当我在前台运行它时,将为每个“丢失的UI共享上下文警告”创建一个新窗口,而孤立的窗口仍然存在。我没有明确创建新的chrome窗口,大概是硒或chrome驱动程序。
我猜测无法删除的文件会附加到一个孤立的进程中,但是我似乎无法弄清楚如何断开连接以便删除它,我需要对代码进行删除功能。
解决丢失的UI共享上下文警告对我来说并不重要,仅因为它可能是权限错误的原因。
这是代码在os.remove(f)
处因PermissionError而崩溃的地方:
# 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')
这是创建硒铬驱动程序的位置,它与发生“丢失UI共享上下文”警告的位置重合,显然是在定义驱动程序时-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)
这是用来完成每个硒铬驱动器的工作的方法:
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()
感谢您的帮助。