【发布时间】:2023-03-12 04:49:01
【问题描述】:
在尝试使用--user-data-dir 和--profile-directory 的用户在Python 3.9.7 上部署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:未知错误:无法删除旧的 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?
【问题讨论】:
-
给 add_arguments 的 最终字符串值 之间的实际区别是什么?没有其他事情重要或与直接的 A-B 相关。
-
不同之处在于,在提供 chrome 浏览器的配置文件路径后,第二个代码将用于创建一个程序,该程序将为用户执行一些自动化工作。 @user2864740
标签: python-3.x debugging selenium-webdriver path selenium4