无法使用 Python 中的 Chrome 的 selenium Web 驱动程序打开两个具有不同配置文件的 Google Chrome 实例

2023-12-02

我正在使用 Selenium WebDriver for Chrome 同时打开两个具有两个不同配置文件(配置文件 1 和配置文件 2)的 Google Chrome 实例。第一个具有配置文件 1 的实例成功打开。但是,当我尝试使用配置文件 2 打开 Chrome 的第二个实例时,出现错误。

这是我的 Python 代码:

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.chrome.options import Options

#Profile Directory for Google Chrome
dataDir = "--user-data-dir=C:\\Users\\Myname\\AppData\\Local\\Google\\Chrome\\User Data"

#Setting the Chrome options for Profile 1
chrome_options1 = Options()
chrome_options1.add_argument(dataDir)
chrome_options1.add_argument("--profile-directory=Profile 1")
driver1 = webdriver.Chrome(chrome_options=chrome_options1)

#This opens www.google.com sucessfully
driver1.get('https://www.google.com')


#Setting the Chrome options for Profile 2
chrome_options2 = Options()
chrome_options2.add_argument(dataDir)
chrome_options2.add_argument("--profile-directory=Profile 2")
#The below line throws an error (Cannot move the Shared Cache)
driver2 = webdriver.Chrome(chrome_options=chrome_options2)

#This line is not reached as there is error in creating driver2 itself 
driver2.get('https://www.google.com')

这是我收到的错误:

[1076:11808:0716/182552:ERROR:cache_util_win.cc(20)] Unable to move the cache: 0

[1076:11808:0716/182552:ERROR:cache_util.cc(134)] Unable to move cache folder C:
\Users\Myname\AppData\Local\Google\Chrome\User Data\ShaderCache\GPUCache to C:\U
sers\Myname\AppData\Local\Google\Chrome\User Data\ShaderCache\old_GPUCache_000

[1076:11808:0716/182552:ERROR:cache_creator.cc(129)] Unable to create cache
[1076:11808:0716/182552:ERROR:shader_disk_cache.cc(589)] Shader Cache Creation failed: -2

我认为该错误是因为 chrome 的第一个实例已锁定共享缓存文件夹(用于写入)。因此,当第二个实例尝试打开同一共享文件夹时,它会引发错误。

有什么解决方法吗?

我的目标是同时打开两个具有两个不同配置文件的 Chrome 实例。

任何帮助表示赞赏。


我不知道这是否仍然与您相关,也不知道我的解决方案是否也适合您,因为您使用 Python,而我使用 R。

我已经使用带有 Chrome Web 驱动程序的 RSelenium 包在 R 中编写了自动 Web 抓取的代码。正如您一样,我想同时使用具有不同 Google Chrome 配置文件的多个 Google Chrome 实例,例如“配置文件 1”和“配置文件 2”,并且我相应地在 Google Chrome 中创建了它们。 R 让我可以轻松地使用两个配置文件之一打开 Selenium Web 驱动程序,例如“配置文件 1”(记住这是 R):

# Define profile directory:
prof1 <- getChromeProfile("~/Users/<username>/AppData/Local/Google/Chrome/User Data", "Profile 1") 

#Set up remote driver with according chrome profile (prof1):
remDr <- remoteDriver(browserName = "chrome", extraCapabilities = prof1) 

#Open remote driver:
remDr$open()

...但绝不能同时使用两个 Google Chrome 配置文件。准确地说,当我使用第二个配置文件(即“配置文件 2”)打开第二个 chrome 实例时,我的控制台以及两个网络驱动程序都冻结了,并且不再恢复。

我的解决方案:

解决方案非常简单:我将 Google Chrome 配置文件文件夹(“配置文件 1”和“配置文件 2”)从默认位置(Google Chrome 创建它们的位置)移动到计算机上的另一个目录,并将它们存储在新创建的父目录中文件夹。让我举个例子:

默认 google chrome 配置文件位置(“配置文件 1”和“配置文件 2”,由 Google Chrome 创建):

"~/Users/<username>/AppData/Local/Google/Chrome/User Data/Profile 1"
"~/Users/<username>/AppData/Local/Google/Chrome/User Data/Profile 2"

我将它们移至“文档”文件夹中的新父文件夹中:

"~/Users/<username>/Documents/Google Chrome Profile 1/Profile 1"
"~/Users/<username>/Documents/Google Chrome Profile 2/Profile 2"

新文件夹“Google Chrome 配置文件 1”和“Google Chrome 配置文件 2”是前面提到的父文件夹。

为什么这有效?

在我看来,默认情况下,Google Chrome 不仅使用各个配置文件文件夹中的配置文件信息,还使用父文件夹中“共享”位置的配置文件信息。如果两个(或更多)配置文件运行来自此类共享文件夹的信息,则可能会变得混乱,相应的网络驱动程序会卡住并且控制台会抛出错误。

这就是为什么在新位置我将配置文件文件夹保存在新的父文件夹“Google Chrome 配置文件 1”和“Google Chrome 配置文件 2”中。像这样,我设法并行运行 4 个具有不同配置文件的独立 chrome 实例(所有实例都有自己的 cookie 和历史记录)。

我希望这对你有用。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

无法使用 Python 中的 Chrome 的 selenium Web 驱动程序打开两个具有不同配置文件的 Google Chrome 实例 的相关文章

随机推荐