为什么 opt.add_argument('--user-data-dir='+r'path') 工作但 opt.add_argument('--user-data-dir='+fr'"{path}" ') 不是 Selenium 的一个选项吗?

Why does opt.add_argument('--user-data-dir='+r'path') work but opt.add_argument('--user-data-dir='+fr'"{path}"') doesn't as an option to Selenium?

我在 Python 3.9.7 上尝试使用 --user-data-dir--profile-directory 部署 chrome driver 时发现了一些非常奇怪的事情,见下文:

如果编译以下代码:

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-dir--profile-directory成功获得了一个chrome驱动程序实例:

现在, cmd:

上使用以下代码杀死所有 chrome 驱动程序实例后
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: unknown error: Could not remove old devtools port file. Perhaps the given user-data-dir at "C:\Users\ResetStoreX\AppData\Local\Google\Chrome\User Data" is still attached to a running Chrome or Chromium process

为什么会这样?

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

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

我想通了,我用 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')

编译此代码后,程序将 运行 不会抛出任何错误,并得到与 post 中显示的第一个代码相同的结果。