小编典典

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

selenium

我正在使用Selenium WebDriver for Chrome打开同时具有两个不同配置文件(配置文件1和配置文件2)的两个Google
Chrome浏览器实例。具有概要文件1的第一个实例成功打开。但是,当我尝试使用Profile 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实例。

任何帮助表示赞赏。


阅读 634

收藏
2020-06-26

共1个答案

小编典典

我不知道这是否仍然与您相关,以及当您使用Python而我使用R时,我的解决方案是否也对您有用。

我已经使用RSelenium包和Chrome网络驱动程序编写了用于R中自动网络抓取的代码。就像您一样,我想同时使用具有不同Google
Chrome个人资料的多个Google Chrome实例,例如“个人资料1”和“个人资料2”,并相应地在Google
Chrome中创建了它们。R让我轻松地打开带有两个配置文件之一的Selenium Web驱动程序,例如“ Profile 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的第二个实例时,我的控制台以及两个Web驱动程序都冻结了,不再恢复了。

我的解决方案:

解决方案非常简单:我将两个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 Profile 1”和“ Google Chrome Profile 2”是前面提到的父文件夹。

为什么这样做?

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

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

希望这对您有用。

2020-06-26