Chromedriver is not deleting the folder named scoped_* at the end of the execution.
Chromedriver not deleting scoped* dir in temp folder after test is complete
The above solution is not working for me, I need a python solution.
My version of google chrome is: 99.0.4844.74 64 bits and I tried chromedriver 99.0.4844.51 and ChromeDriver 99.0.4844.35
Also tried this code at the end of every script, after driver.close() and driver.quit():
try:
for f in glob.glob(r"C:\Windows\Temp\scoped_dir*"):
shutil.rmtree(f)
except Exception as e:
print(e)
output:
[WinError 5] Access is denied: 'C:\Windows\Temp\scoped_dir130892_1670710986\BrowserMetrics\BrowserMetrics-62347334-8E820.pma'
Also tried to run a batch file with the following code:
@echo off
Taskkill /IM chromedriver.exe /F
Taskkill /IM chrome.exe /F
cd /D %Temp%
for /d %%D in (scoped_dir*) do rd /s /q "%%D"
del /f /q *
output:
SUCCESS: The process "chrome.exe" with PID 406028 has been terminated.
but it doesn't delete the folders.
CodePudding user response:
[WinError 5] Access is denied: 'C:\Windows\Temp\scoped_dir130892_1670710986\BrowserMetrics\BrowserMetrics-62347334-8E820.pma'
The reason why you are getting this error is because windows doesn't allow that python script from altering files that are in the C:\Windows folder. Even with being the administrator account, you are going to have to run the file as an administrator. You can do this by right-clicking on the .py file and clicking on Run as administrator
. This will allow the script to alter the files in the C:\Windows directory.
If denial of access persists, take a look at this question. Another solution to this problem might be to install pydirectory.
CodePudding user response:
This error message...
[WinError 5] Access is denied: 'C:\Windows\Temp\scoped_dir130892_1670710986\BrowserMetrics\BrowserMetrics-62347334-8E820.pma'
...implies that some child still continues to access BrowserMetrics-62347334-8E820.pma
. Hence shutil.rmtree()
fails with WinError as the file is still accessed by some other process.
Deep Dive
Although your program is at the end of the execution but as @[email protected]
mentions:
This appears to be a race condition between ChromeDriver and Chrome. ChromeDriver creates these temp directories for use by Chrome, and at the end ChromeDriver tries to delete those directories. ChromeDriver waits for the main Chrome process to terminate before doing the deletion, but some Chrome child processes might still be running and holding on to those directories, causing the deletion to fail. Currently ChromeDriver doesn't retry the deletion. Adding some retries might be the easiest fix.
This issue was addressed through this revision / commit:
[Chromedriver] Retry deleting temp dir when needed
Occasionally ChromeDriver may fail to delete temporary directories while exiting, causing wasted disk space. This change adds retry logic while cleaning up these directories.
and was available with ChromeDriver v2.30 and with ChromeDriver v99.0.4844.51 this issue seems to be a regression.
tl; dr
WebDriver is not deleting the profile directory after test exits