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=/account')
您使用相應的--user-data-dirand成功獲得了一個 chrome 驅動程式實體--profile-directory:

現在,在使用以下代碼殺死所有 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=/account')
最后輸入: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 ?
uj5u.com熱心網友回復:
我想通了,我正在創建一個語法錯誤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=/account')
編譯此代碼后,程式將運行而不會引發任何錯誤,并獲得與本文中顯示的第一個代碼相同的結果。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/418480.html
標籤:
上一篇:滾動條重復winapic
下一篇:Swing-GUIGPA計算器
