0

chrome driver在尝试部署using--user-data-dir--profile-directoryfrom the user on时,我意识到了一些非常奇怪的事情Python 3.9.7,见下文:

如果编译以下代码:

from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.chrome.service import Service

opt = Options() #the variable that will store the selenium options

opt.add_argument('--user-data-dir='+r'C:\Users\ResetStoreX\AppData\Local\Google\Chrome\User Data') #Add the user data path as an argument in selenium Options
opt.add_argument('--profile-directory=Default') #Add the profile directory as an argument in selenium Options
s = Service('C:/Users/ResetStoreX/AppData/Local/Programs/Python/Python39/Scripts/chromedriver.exe')

driver = webdriver.Chrome(service=s, options=opt) 
driver.get('https://opensea.io/login?referrer=%2Faccount')

您使用相应的--user-data-dirand成功获得了一个 chrome 驱动程序实例--profile-directory

预览1

现在,在使用以下代码杀死所有 chrome 驱动程序实例后 cmd

taskkill /F /IM chromedriver.exe

然后编译这个其他代码:

from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.chrome.service import Service

opt = Options() #the variable that will store the selenium options

path = input('Introduce YOUR profile path:')

opt.add_argument('--user-data-dir='+fr'"{path}"') #Add the user data path as an argument in selenium Options
opt.add_argument('--profile-directory=Default') #Add the profile directory as an argument in selenium Options
s = Service('C:/Users/ResetStoreX/AppData/Local/Programs/Python/Python39/Scripts/chromedriver.exe')

driver = webdriver.Chrome(service=s, options=opt) 
driver.get('https://opensea.io/login?referrer=%2Faccount')

最后输入:C:\Users\ResetStoreX\AppData\Local\Google\Chrome\User Data作为输入

你得到这个错误

WebDriverException:未知错误:无法删除旧的 devtools 端口文件。可能“C:\Users\ResetStoreX\AppData\Local\Google\Chrome\User Data”中给定的用户数据目录仍附加到正在运行的 Chrome 或 Chromium 进程

为什么会这样?

不是opt.add_argument('--user-data-dir='+fr'"{path}"')传递此用户数据路径的有效方式:

path = C:\Users\ResetStoreX\AppData\Local\Google\Chrome\User Data?

4

1 回答 1

0

我想通了,我正在创建一个语法错误opt.add_argument('--user-data-dir='+fr'"{path}"'),所以我将其更改为opt.add_argument('--user-data-dir='+fr'{path}'),改进后的代码如下:

from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.chrome.service import Service

opt = Options() #the variable that will store the selenium options

path = input('Introduce YOUR profile path:')

opt.add_argument('--user-data-dir='+fr'{path}') #Add the user data path as an argument in selenium Options
opt.add_argument('--profile-directory=Default') #Add the profile directory as an argument in selenium Options
s = Service('C:/Users/ResetStoreX/AppData/Local/Programs/Python/Python39/Scripts/chromedriver.exe')

driver = webdriver.Chrome(service=s, options=opt) 
driver.get('https://opensea.io/login?referrer=%2Faccount')

编译此代码后,程序将运行而不会引发任何错误,并获得与本文中显示的第一个代码相同的结果。

于 2022-01-19T03:33:01.407 回答