我不知道这是否仍然与您相关,也不知道我的解决方案是否也适合您,因为您使用 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 和历史记录)。
我希望这对你有用。