无法在Python中使用适用于Chrome的selenium网络驱动程序打开两个具有不同配置文件的Google Chrome实例 [英] Cannot open two Google Chrome instances with different profiles using selenium web driver for Chrome in Python

查看:286
本文介绍了无法在Python中使用适用于Chrome的selenium网络驱动程序打开两个具有不同配置文件的Google Chrome实例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



这是我的Python代码:

  from selenium import webdriver 
from selenium.webdriver.common.keys import selenium.webdriver.chrome.options中的键
import选项

#谷歌浏览器的配置文件目录
dataDir =--user-data-dir = C:\\ Users \\ My Name \\AppData\\Local\\\ \\ Google \\Chrome\\User Data

#为配置文件1设置Chrome选项
chrome_options1 =选项()
chrome_options1.add_argument(dataDir)
chrome_options1.add_argument( - profile-directory = Profile 1)
driver1 = webdriver.Chrome(chrome_options = chrome_options1)

#This打开www.google.com成功
driver1.get('https://www.google.com')


#为配置文件2设置Chrome选项
chrome_options2 =选项()
chrome_options2.add_argument(dataDir )
chrome_options2.add_argument( - profile-directory = Profile 2)
#下面一行会引发错误(无法移动共享缓存)
driver2 = webdriver.Chrome(chrome_options = chrome_options2)

#由于在创建驱动程序2本身时出现错误,因此未达到此行
driver2.get('https://www.google.com')

以下是我收到的错误:

  [1076:11808:0716/182552:错误:cache_util_win.cc(20)]无法移动缓存:0 

[1076:11808:0716/182552:错误:cache_util .cc(134)]无法将缓存文件夹C:
\ Users \ My Name\AppData\Local\Google\Chrome\User Data\ShaderCache\GPUCache移动到C:\ U
sers\Myname\AppData\Local\Google\Chrome\User Data\ShaderCache\old_GPUCache_000

[1076:11808:0716/182552:错误: cache_creator.cc(129)]无法创建缓存
[1076:11808:0716/182552:错误:shader_disk_cache.c c(589)] Shader Cache Creation failed:-2

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



有没有解决方法?



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



任何帮助都是赞赏。

解决方案

我不知道这是否与您仍然相关,如果我的解决方案也适用于您,而您使用了Python,而我使用了R。 b

我使用RSelenium软件包和Chrome网络驱动程序编写了一个用于R中自动化Web抓取的代码。就像你一样,我想同时使用Google Chrome浏览器的多个实例,并使用不同的Google Chrome浏览器个人资料,例如个人资料1和个人资料2,并且我在Google Chrome中相应地创建了他们。 R让我用两个配置文件之一轻松打开硒驱动程序,例如Profile 1(记住这是R):

$ p $ #定义配置文件目录:
prof1< - getChromeProfile( 〜/ Users /< username> / AppData / Local / Google / Chrome / User Data,Profile 1)

#根据铬配置文件(prof1)设置远程驱动程序:
remDr < - remoteDriver(browserName =chrome,extraCapabilities = prof1)

#打开远程驱动程序:
remDr $ open()

...但从来没有Google Chrome个人资料同时使用。确切地说,只要我用第二个配置文件(即配置文件2)打开第二个chrome实例,我的控制台以及两个网络驱动程序都会冻结并且不会再恢复。



我的解决方案:

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



默认的Google Chrome配置文件位置(由Google Chrome创建的配置文件1和配置文件2):



pre $ 〜/ 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个人资料1 /个人资料1
〜/用户/<用户名> /文档/ Google Chrome个人资料2 /个人资料2

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



为什么这有效?

在我看来,每个默认Google Chrome不仅使用各个配置文件文件夹中的配置文件信息,而且还使用父文件夹中的共享位置。如果两个(或更多)配置文件运行来自这样的共享文件夹的信息,它可能会变得混乱,相应的Web驱动程序会卡住,并且控制台会报错。



为什么在新的位置我将配置文件夹保存在新的父文件夹Google Chrome配置文件1和Google Chrome配置文件2中。就像这样,我可以并行运行4个独立的chrome实例(它们都有自己的cookie和历史记录)。



我希望这对你有用。 / p>

I am using Selenium WebDriver for Chrome to open two instances of Google Chrome with two different Profiles (Profile 1 and Profile 2) simultaneously. The first instance with Profile 1 opens successfully. But when I try to open the second instance of Chrome with Profile 2, I get an error.

Here's my Python code:

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')

Here's the error I'm getting:

[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

I think the error is because the first instance of chrome has locked the shared cache folder (for writing). Therefore, When the second instance tries to open the same shared folder it throws an error.

Is there any workaround for this?

My goal is to open two Chrome instances with two different Profiles simultaneously.

Any help is Appreciated.

解决方案

I don't know if this is still relevant to you and if my solution also works for you as you used Python whereas I use R.

I have written a code for automated web-scraping in R using the RSelenium package with Chrome web-driver. Just as you, I wanted to simultaneously use multiple instances of Google Chrome with different Google Chrome Profiles, say "Profile 1" and "Profile 2", and I created them in Google Chrome accordingly. R let me easily open a selenium web-driver with one of the two profiles, e.g. "Profile 1" (remember this is 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()

...but never both Google Chrome profiles at the same time. To be precise, as soon as I opened a second instance of chrome with the second profile, i.e. "Profile 2", my console as well as both web-drivers froze and did not recover any more.

My Solution:

The solution was quite simple: I moved both Google Chrome profile folders ("Profile 1" and "Profile 2") from their default location (where Google Chrome created them) to a different directory on my computer and stored them in a newly created parent folder. Let me provide an example:

Default google chrome profile locations ("Profile 1" and "Profile 2", created by Google Chrome):

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

I moved them to my "Documents" folder into new parent folders:

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

The new folders "Google Chrome Profile 1" and "Google Chrome Profile 2"are the before mentioned parent folders.

Why does this work?

It seems to me that per default Google Chrome not only uses profile information from the respective profile folders but also from the "shared" location in the parent folder. If two (or more) profiles run information from such a shared folder, it can get messy, the corresponding web-drivers get stuck and the console throws an error.

This is why in the new location I saved the profile folders in the new parent folders "Google Chrome Profile 1" and "Google Chrome Profile 2". Like this I manage to run 4 independent chrome instances with different profiles in parallel (all of them with their own cookies and history).

I hope this works for you.

这篇关于无法在Python中使用适用于Chrome的selenium网络驱动程序打开两个具有不同配置文件的Google Chrome实例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆