問題描述
當我嘗試使用 --user-data-dir
讓當前用戶使用 Selenium 啟動 Chrome 時,我收到以下錯誤:
When I am trying to use --user-data-dir
for the current user to start Chrome using Selenium I am getting an error as:
File "C:Program Files (x86)Pythonlibsite-packagesseleniumwebdriver
emotewebdriver.py", line 321, in execute
self.error_handler.check_response(response)
File "C:Program Files (x86)Pythonlibsite-packagesseleniumwebdriver
emoteerrorhandler.py", line 242, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.InvalidArgumentException: Message: invalid argument: user data directory is already in use, please specify a unique value for --user-data-dir argument, or don't use --user-data-dir
我該如何解決這個錯誤?
How can I fix this error?
推薦答案
這個錯誤信息...
selenium.common.exceptions.InvalidArgumentException: Message: invalid argument: user data directory is already in use, please specify a unique value for --user-data-dir argument, or don't use --user-data-dir
...暗示 ChromeDriver 無法使用指定的 用戶數據目錄
啟動新的 Chrome 瀏覽器 會話,因為它已經正在使用中.
...implies that the ChromeDriver was unable to initiate the new Chrome Browser session using the specified user data directory
as it was already in use.
這個錯誤可以重現如下:
This error can be reproduced as follows:
代碼塊:
Code Block:
from selenium import webdriver
import getpass
options = webdriver.ChromeOptions()
options.add_argument("start-maximized")
options.add_argument(r"--user-data-dir=C:Users{}AppDataLocalGoogleChromeUser Data".format(getpass.getuser()))
driver = webdriver.Chrome(options=options, executable_path=r'C:WebDriverschromedriver.exe')
driver.get("https://www.google.com/")
完成相關回溯:
Complete relevant traceback:
[12148:21412:0204/035557.731:ERROR:cache_util_win.cc(21)] Unable to move the cache: Access is denied. (0x5)
[12148:21412:0204/035557.731:ERROR:cache_util.cc(141)] Unable to move cache folder C:UsersSoma BhattacharjeeAppDataLocalGoogleChromeUser DataShaderCacheGPUCache to C:UsersSoma BhattacharjeeAppDataLocalGoogleChromeUser DataShaderCacheold_GPUCache_000
[12148:21412:0204/035557.731:ERROR:disk_cache.cc(178)] Unable to create cache
[12148:21412:0204/035557.731:ERROR:shader_disk_cache.cc(605)] Shader Cache Creation failed: -2
Opening in existing browser session.
Traceback (most recent call last):
File "C:UsersSoma BhattacharjeeDesktopDebanjanPyProgramsyandex_ru.py", line 18, in <module>
driver = webdriver.Chrome(options=options, executable_path=r'C:WebDriverschromedriver.exe')
File "C:Pythonlibsite-packagesseleniumwebdriverchromewebdriver.py", line 81, in __init__
desired_capabilities=desired_capabilities)
File "C:Pythonlibsite-packagesseleniumwebdriver
emotewebdriver.py", line 157, in __init__
self.start_session(capabilities, browser_profile)
File "C:Pythonlibsite-packagesseleniumwebdriver
emotewebdriver.py", line 252, in start_session
response = self.execute(Command.NEW_SESSION, parameters)
File "C:Pythonlibsite-packagesseleniumwebdriver
emotewebdriver.py", line 321, in execute
self.error_handler.check_response(response)
File "C:Pythonlibsite-packagesseleniumwebdriver
emoteerrorhandler.py", line 242, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.InvalidArgumentException: Message: invalid argument: user data directory is already in use, please specify a unique value for --user-data-dir argument, or don't use --user-data-dir
錯誤堆棧跟蹤清楚地抱怨 訪問被拒絕,因為程序無法將緩存文件夾 ..ShaderCacheGPUCache
移動到 ..ShaderCacheold_GPUCache_000
.因此 cache 創建失敗,隨后 Shader Cache Creation 創建失敗.雖然這些問題會引發 InvalidArgumentException
,但可以在現有 Chrome 瀏覽器會話中強制打開一個新窗口.
The error stack trace clearly complains of Access is denied as the program was unable to move the cache folder ..ShaderCacheGPUCache
to ..ShaderCacheold_GPUCache_000
. hence the creation of cache failed and subsequently creation of Shader Cache Creation failed. Though these issues raises the InvalidArgumentException
but forcefully able to open a new window within the existing Chrome Browser Session.
雖然拋出了錯誤,但新的 Chrome 窗口仍會啟動,但仍與已打開的 Chrome 會話連接,但新窗口無法由 WebDriver 實例.因此,您會在 url 欄中看到 data:,
.
Though the error is thrown, still the new Chrome window gets initiated but remains attached with the already opened Chrome session but the new window can't be controlled by the WebDriver instance. Hence you see data:,
in the url bar.
你需要注意幾件事:
- 如果您使用 默認 Chrome 配置文件在同一臺測試機上訪問其他工作的網頁,則不應設置
user-data-dir
作為用戶數據,因為它仍然被您手動啟動的其他 Chrome 進程鎖定.- 在上述場景中,您需要創建和使用另一個 Chrome 配置文件,您可以在 如何通過 Python 打開 Chrome 配置文件
- If you are using the Default Chrome Profile to access webpages for your other work on the same Test Machine, you shouldn't set
user-data-dir
as the User Data as it remains locked by the other Chrome process you have initiated manually.- In the above scenario you need to create and use another Chrome Profile and you can find a detailed discussion in How to open a Chrome Profile through Python
- 在上述場景中,您需要創建和使用另一個 Chrome 配置文件,您可以在 如何在 Selenium Webdriver Python 3 中使用 Chrome 配置文件
- In the above scenario you need to create and use another Chrome Profile and you can find a detailed discussion in How to use Chrome Profile in Selenium Webdriver Python 3
- 您可以在 如何通過 Selenium 的 --user-data-dir 參數打開 Chrome 配置文件
這篇關于InvalidArgumentException:消息:無效參數:用戶數據目錄已在使用錯誤使用 --user-data-dir 使用 Selenium 啟動 Chrome的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!
【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!