問題描述
我正在嘗試使用我的默認用戶使用 selenium python 庫打開一個網頁,腳本使用默認用戶很重要,但如果我的 chrome 瀏覽器已經打開,腳本會崩潰并給我這個錯誤:
selenium.common.exceptions.InvalidArgumentException:消息:無效參數:用戶數據目錄已在使用中,請為 --user-data-dir 參數指定唯一值,或者不要使用 --user-數據目錄
我已經嘗試了這里給出的所有解決方案:
現有 Chrome 瀏覽器會話中的新窗口快照:
<小時>結論
雖然錯誤仍然被拋出,但新的 Chrome 窗口仍會啟動,但仍與已打開的 Chrome 會話連接,但新窗口無法由 WebDriver 實例.
<小時>解決方案
你需要注意幾件事:
- 如果您使用 默認 Chrome 配置文件在同一臺測試機上訪問其他工作的網頁,則不應設置
user-data-dir
作為用戶數據,因為它仍然被您手動啟動的其他 Chrome 進程鎖定.- 在上述場景中,您需要創建和使用另一個 Chrome 配置文件,您可以在 如何通過 Python 打開 Chrome 配置文件
- 如果你在一個獨立的測試系統中執行你的測試,你可以將
user-data-dir
設置為..User DataDefault 以訪問默認 Chrome 配置文件.- 在上述場景中,您需要創建和使用另一個 Chrome 配置文件,您可以在 如何在 Selenium Webdriver Python 3 中使用 Chrome 配置文件
- 但是,根據最佳做法,您必須始終創建一個新的 Chrome 配置文件來執行您的測試,因為 默認 Chrome 配置文件可能包含 擴展程序,書簽、瀏覽歷史等,可能無法正常加載.
- 您可以在 如何通過 Selenium 的 --user-data-dir 參數打開 Chrome 配置文件
I am trying to open a web page using the selenium python library with my default user, it is critical that the script uses the default user but if my chrome browser is already open the script crashes and gives me 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
I have tried all the solutions given here :
Selenium chromedriver won't launch URL if another chrome instance is open
Selenium won't open a new URL in a new tab (Python & Chrome)
and read that there was a bug in older chromedriver versions but it was fixed in chrome 74 (which im using ) : https://github.com/SeleniumHQ/docker-selenium/issues/741
from selenium import webdriver
import time
from getpass import getuser
def run():
# Chrome driver path
chromedriver = r'C:Usersuser1Downloadschromedriver_win32chromedriver_new.exe'
# Get chrome webdriver options and set open the browser as headless
chrome_options = webdriver.ChromeOptions()
#chrome_options.add_argument("--headless")
# Fix for selenium Issue 2907
#chrome_options.add_argument('--log-level=3')
#chrome_options.add_experimental_option('excludeSwitches', ['enable-logging'])
# Load current user default profile
current_user = getuser()
chrome_options.add_argument(
r"--user-data-dir=C:Users{}AppDataLocalGoogleChromeUser Data".format(current_user))
# didable "Chrome is being controled by an automated test software"
chrome_options.add_argument('disable-infobars')
# get Chrome to stay open
chrome_options.add_experimental_option("detach", True)
# open browser with options and driver
driver = webdriver.Chrome(options=chrome_options, executable_path=chromedriver)
driver.get(r'https://www.youtube.com/watch?v=dQw4w9WgXcQ')
if __name__ == '__main__':
run()
if i run it without a chrome browser open its fine if not it crashes
This error message...
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
...implies that the ChromeDriver was unable to initiate/spawn a new Browsing Context i.e. Chrome Browser session using the specified user data directory
as it was already in use.
I was able to reproduce the error in my local windows-10box 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:
[18516:23156:0204/032227.883:ERROR:cache_util_win.cc(21)] Unable to move the cache: Access is denied. (0x5) [18516:23156:0204/032227.898:ERROR:cache_util.cc(141)] Unable to move cache folder C:UsersSoma BhattacharjeeAppDataLocalGoogleChromeUser DataShaderCacheGPUCache to C:UsersSoma BhattacharjeeAppDataLocalGoogleChromeUser DataShaderCacheold_GPUCache_000 [18516:23156:0204/032227.898:ERROR:disk_cache.cc(178)] Unable to create cache [18516:23156:0204/032227.898:ERROR:shader_disk_cache.cc(605)] Shader Cache Creation failed: -2 Opening in existing browser session. Traceback (most recent call last): . 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
Analysis
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.
Snapshot of existing Chrome Browser Session:
Snapshot of new window within the existing Chrome Browser Session:
Conclusion
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.
Solution
You need to take care of a couple of things:
- 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
- If you are executing your tests in a isolated test system, you can set
user-data-dir
as ..User DataDefault to access the Default Chrome Profile.- 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
- However as per best practices you must always create a new Chrome Profile to execute your tests as the Default Chrome Profile may contain Extensions, Bookmarks, Browsing History, etc, and may not load properly.
- You can find a detailed discussion in How to open a Chrome Profile through --user-data-dir argument of Selenium
這篇關于InvalidArgumentException:消息:無效參數:使用 ChromeDriver Selenium 啟動 Chrome 時,用戶數據目錄已在使用中出錯的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!