Python Selenium不使用默认的Chrome配置文件
Selenium将不会加载我的默认Chrome配置文件,我无法找出原因。我尝试了配置文件1和默认配置文件,但出现了相同的错误(见下文)。我已经与任务管理器确认,在运行此代码之前,所有Chrome窗口都已关闭。有什么想法吗Python Selenium不使用默认的Chrome配置文件,python,selenium,google-chrome,selenium-chromedriver,chrome-profile,chromium,Python,Selenium,Google Chrome,Selenium Chromedriver,Chrome Profile,Chromium,Selenium将不会加载我的默认Chrome配置文件,我无法找出原因。我尝试了配置文件1和默认配置文件,但出现了相同的错误(见下文)。我已经与任务管理器确认,在运行此代码之前,所有Chrome窗口都已关闭。有什么想法吗 from selenium import webdriver from bs4 import BeautifulSoup import pandas as pd import numpy as np from webdriver_manager.chrome import Ch
from selenium import webdriver
from bs4 import BeautifulSoup
import pandas as pd
import numpy as np
from webdriver_manager.chrome import ChromeDriverManager
from selenium.webdriver.chrome.options import Options
import os
os.system("taskkill /f /im geckodriver.exe /T")
os.system("taskkill /f /im chromedriver.exe /T")
os.system("taskkill /f /im IEDriverServer.exe /T")
os.system("taskkill /f /im chrome.exe /T")
driver2 = r"C:\Users\xxx\.wdm\drivers\chromedriver\87.0.4280.20\win32\chromedriver.exe"
options = webdriver.ChromeOptions()
options.add_argument("user-data-dir=C:\\Users\xxx\\AppData\\Local\\Google\\Chrome\\User Data\\Profile 1")
driver = webdriver.Chrome(executable_path=driver2, chrome_options=options)
driver.get("https://www.google.co.in")
回溯(最近一次呼叫最后一次):
文件“C:\Users\xxx\OneDrive\Python\pyReportRun.py”,第16行,在
driver=webdriver.Chrome(ChromeDriverManager().install(),options=options)
文件“C:\Python38\lib\site packages\selenium\webdriver\chrome\webdriver.py”,第76行,在\uuu init中__
RemoteWebDriver.\uuu init__(
文件“C:\Python38\lib\site packages\selenium\webdriver\remote\webdriver.py”,第157行,在\uuu init中__
启动会话(功能、浏览器配置文件)
文件“C:\Python38\lib\site packages\selenium\webdriver\remote\webdriver.py”,第252行,在启动会话中
响应=self.execute(Command.NEW_会话,参数)
文件“C:\Python38\lib\site packages\selenium\webdriver\remote\webdriver.py”,第321行,执行
self.error\u handler.check\u响应(响应)
文件“C:\Python38\lib\site packages\selenium\webdriver\remote\errorhandler.py”,第242行,在check\u响应中
引发异常类(消息、屏幕、堆栈跟踪)
selenium.common.exceptions.WebDriverException:消息:未知错误:无法删除旧的devtools端口文件。可能位于C:\Users\xxx\AppData\Local\Google\Chrome\user data\Profile 1的给定用户数据目录仍连接到正在运行的Chrome或Chrome进程
用户数据目录将配置文件视为默认配置文件,您不必指定它。如果它是其他内容,请通过配置文件目录参数指定它
创建配置文件的步骤:
chrom_options.add_argument("user-data-dir=c:\\tmp\\newdir")
chrom_options.add_argument("profile-directory=Profile 1")
打开:chrome://version 地址栏中的
将用户目录文件夹完全复制到例如c:\tmp\newdir
打开复制的用户数据(newdir)并搜索名为Default的文件夹。这是配置文件文件夹
将默认文件夹重命名为“配置文件1”
现在使用这个:
chrom_options.add_argument("user-data-dir=c:\\tmp\\newdir")
chrom_options.add_argument("profile-directory=Profile 1")
此错误消息
selenium.common.exceptions.WebDriverException: Message: unknown error: Could not remove old devtools port file. Perhaps the given user-data-dir at C:\Users\xxx\AppData\Local\Google\Chrome\User Data\Profile 1 is still attached to a running Chrome or Chromium process
…表示ChromeDriver无法启动/生成新的浏览上下文,即Chrome浏览器会话,因为您已传递参数的用户数据目录
仍附加到正在运行的Chrome浏览上下文
细节 用于测试自动化将违反所有最佳实践,因为默认Chrome配置文件可能包含以下任一/所有内容:
- 浏览器设置
- 扩展
- 书签
- 应用程序
- 保存的密码
- 浏览历史记录
- 等
默认Chrome配置文件的位置 根据文档,Chrome默认配置文件文件夹的位置因平台而异。位置为:
- Windows 7、8.1和10:
C:\Users\\AppData\Local\Google\Chrome\User Data\Default
- Mac OS X El Capitan:
用户//库/应用程序支持/Google/Chrome/Default
- Linux:
/home/.config/google chrome/default
替换为用户文件夹的名称。默认配置文件文件夹的名称仅为default(或Linux中的default)。但是,如果您创建了其他配置文件,它们的文件夹名称就不那么明显。创建配置文件时分配给它的名称显示在Chrome窗口标题栏右侧的名称按钮上。不幸的是,Chrome在关联的配置文件文件夹上使用的名称是一个通用的编号名称,如Pr文件3
如果您需要知道Chrome配置文件的文件夹名称,只需访问chrome://version
,然后按Enter键
快照:
配置文件路径
显示当前配置文件的位置。例如,我的默认配置文件在我的Windows 10系统中的位置是C:\Users\Soma Bhattacharjee\AppData\Local\Google\Chrome\User Data\Default
。您可以选择路径并复制它,然后将其粘贴到Windows中的文件资源管理器中,查找器打开OSX或Linux中的Nautilus之类的文件管理器,以访问该文件夹
示例代码(Windows 10) 最后,要访问默认的Chrome配置文件,您可以使用以下基于Python的解决方案:
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
options = webdriver.ChromeOptions()
options.add_argument("user-data-dir=C:\\Users\\username\\AppData\\Local\\Google\\Chrome\\User Data\\Default")
driver = webdriver.Chrome(executable_path=r'C:\WebDrivers\chromedriver.exe', chrome_options=options)
driver.get("https://www.google.co.in")
您可以在中找到详细的讨论
添加时,我仍然看不到我的个人资料。浏览器仍然没有登录,chrome//版本显示:C:\Users\ROBERT~1.CAR\AppData\Local\Temp\scoped\u dir45504\u 1195749914\profile 1您添加了什么,添加了您使用的脚本您应该将用户目录添加为C:\Users\ROBERT~1.CAR\AppData\Local\Temp\scoped\u dir45504\u 1195749914和profiledir as Profile 1很抱歉,这可能会有帮助…我尝试了:options.add_参数(“Profile directory=Profile 1”)和options.add_参数(“Profile directory=C:\\Users\robert.car\\AppData\\Local\\Google\\Chrome\\User Data\\Profile 1”)更新了答案使用用户目录和配置文件作为showmI我不尝试引用我的默认配置文件,而是由C:\Users\robert.car\AppData\Local\Google\Chrome\user Data\profile 1提供的配置文件1。每当添加以下配置文件时,我们都会收到错误
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
options = webdriver.ChromeOptions()
options.add_argument("user-data-dir=C:\\Users\\username\\AppData\\Local\\Google\\Chrome\\User Data\\Default")
driver = webdriver.Chrome(executable_path=r'C:\WebDrivers\chromedriver.exe', chrome_options=options)
driver.get("https://www.google.co.in")