当前位置: 首页 > 面试题库 >

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

南门正祥
2023-03-14
问题内容

我正在使用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实例。

任何帮助表示赞赏。


问题答案:

我不知道这是否仍然与您相关,以及当您使用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和历史记录)。

希望这对您有用。



 类似资料:
  • 我试图打开两个实例ChromeDriver具有相同的配置文件如下: 问题是第一个驱动程序工作并导航到Google,但在第二个驱动程序中,我在实例化第二个驱动程序时遇到以下异常: 附加信息:抛出了一个带有空响应的异常,该异常向远程WebDriver服务器发送HTTP请求以获取URLhttp://localhost:6949/session.异常的状态为ReceiveFailure,消息为:基础连接已

  • 我尝试过不同的URI方案: 以下内容根本不起作用: null null

  • 问题内容: 当他们都使用上下文管理器时,我很难弄清楚如何模拟类中的两个文件打开。我知道如何使用模拟模块对一个上下文管理的文件执行以下操作: 我的问题是当一个类在同一调用中打开两个不同的文件时如何执行此操作。就我而言,该类将文件预加载到两个映射中。此类在其他类别中使用。我想模拟这两个文件的加载以提供我的测试数据,以便可以对我使用预加载的测试文件内容来测试使用IfAddrConfig对象的其他类。 这

  • 我试图启动firefox浏览器,并使用selenium webdriver打开一个网页示例< code>google.com。我成功地启动了浏览器,但启动网页时出现了一些问题。我得到的错误是 线程“main”中的异常 org . open QA . selenium . remote . unreachablebrowserexception:无法启动新会话。 可能的原因是远程服务器地址无效或浏览

  • 问题内容: 执行上述代码时显示错误: 线程“主”中的异常org.openqa.selenium.remote.SessionNotFoundException:启动Internet Explorer发生意外错误。所有区域的保护模式设置都不相同。对于所有区域,“启用保护模式”必须设置为相同的值(启用或禁用)。(警告:服务器未提供任何堆栈跟踪信息)命令持续时间或超时:1.33秒 有人可以建议解决此问题

  • 我试图使用Selenium Webdriver python单击网站上的一个按钮,我看到该按钮高亮显示,但没有使用函数单击。 下面是我尝试单击的按钮的html代码 我正在尝试使用id单击按钮 我还尝试了xpath,得到了相同的结果