我有一個程式會詢問用戶profile path來自 Chrome 驅動程式,在提供它并單擊user_data按鈕然后單擊open_browser按鈕后,它將使用用戶資料和組態檔目錄打開 Chrome 驅動程式,這里:
import tkinter as tk
from tkinter import ttk #for user inputs
from tkinter import messagebox #for warning messages
import re
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.chrome.service import Service
root = tk.Tk()
root.geometry('500x400') #resolution
root.title("Bulkdozer") #Name of this program
root.attributes('-topmost', True) #keep the program's window top-most
opt = Options() #the variable that will store the selenium options
def submit_profile_path():
if len(profile_path.get()) == 0: #check if the user didn't type anything and pressed the button
messagebox.showerror(message="You didn't provide any input, try again", title="NULL Input")
elif len(profile_path.get()) > 0:
if r'\Google\Chrome\User Data' in profile_path.get(): #check if the path provided by the user is a valid one
if profile_path.get().split("User Data\\",1)[1] != "": #now check if at the end of that path exist an actual profile folder
user_data.pack_forget() #hide the user_data button
data_input.pack_forget() #hide the data_input
profile_path_label.pack_forget() #hide the profile_path_label
open_browser.pack() #show the open_browser button
x = profile_path.get() #get the profile path
y = x.replace(re.split('\\bUser Data\\b',x)[-1], "") #get the user data path
z = x.split("User Data\\",1)[1] #get the profile directory
global opt
opt.add_argument(fr'--user-data-dir="{y}"') #Add the user data path as an argument in selenium Options
print(y)
opt.add_argument(f'--profile-directory={z}') #Add the profile directory as an argument in selenium Options
print(z)
print(type(opt))
print(opt)
return opt
else: #inform the user that he must provide the profile path containing the corresponding profile folder
messagebox.showwarning(message="You forgot to add the PROFILE FOLDER in the profile path, try again", title="Profile Folder Missing")
data_input.delete(0, tk.END)
else: #inform the user that he must provide a valid profile path
messagebox.showwarning(message="The path provided does not seem to be the right one, try again", title="Invalid Profile PATH")
data_input.delete(0, tk.END)
# BUTTON FOR PROVIDING THE PROFILE PATH OF CHROME BROWSER #
profile_path = tk.StringVar() #This variable will be used for storing the profile path string passed by the user
signin = ttk.Frame(root) #create a container for the profile_path variable
signin.pack(padx=55, pady=20, fill='x', expand=True) #define the dimensional measurement and location for this container
profile_path_label = ttk.Label(signin, text="Introduce YOUR profile path:") #create a label for the profile_path variable
profile_path_label.pack(fill='x', expand=True) #add the label
data_input = ttk.Entry(signin, textvariable=profile_path) #create an entry for the profile_path variable
data_input.pack(fill='x', expand=True)
data_input.focus()
user_data = tk.Button(root, width=20, text="Submit User Data", command=submit_profile_path) #executes the function when clicked
user_data.place(x=60, y=40, width=100, height=30) #define the dimensional measurement and location for this button
user_data.pack() #Apply The Pack geometry manager to this button for using its functions later on
#opt.add_argument('--user-data-dir=' r'C:\Users\ResetStoreX\AppData\Local\Google\Chrome\User Data') #PATH profile
#opt.add_argument('--profile-directory=Default')
s = Service('C:/Users/ResetStoreX/AppData/Local/Programs/Python/Python39/Scripts/chromedriver.exe')
######################## BUTTON ZONE #############################
def open_chrome_profile():
driver = webdriver.Chrome(service=s, options=opt)
driver.get('https://opensea.io/login?referrer=/account')
# BUTTON FOR OPENING A CHROME DRIVER WITH THE OPTIONS PASSED #
open_browser = tk.Button(root, width=20, text="Open OpenSea Tab", command=open_chrome_profile) #executes the function when clicked
open_browser.place(width=100, height=30) #define the dimensional measurement and location for this button
open_browser.pack() #Apply The Pack geometry manager to this button for using its functions later on
open_browser.pack_forget() #initialize this button hidden
###################### BUTTON ZONE END ###########################
root.mainloop()
我正在使用以下組態檔路徑測驗我的程式:
C:\Users\ResetStoreX\AppData\Local\Google\Chrome\User Data\Default
在單擊user_data按鈕然后單擊open_browser按鈕后,我收到以下錯誤:
selenium.common.exceptions.WebDriverException:訊息:未知錯誤:無法洗掉舊的 devtools 埠檔案。可能“C:\Users\ResetStoreX\AppData\Local\Google\Chrome\User Data”中給定的用戶資料目錄仍附加到正在運行的 Chrome 或 Chromium 行程
這是非常出乎意料的,因為在測驗該程式時我沒有打開任何 Chrome 瀏覽器,并且如果在下面設定以下行(而不是我目前擁有的global opt行)submit_profile_path():
opt.add_argument('--user-data-dir=' r'C:\Users\ResetStoreX\AppData\Local\Google\Chrome\User Data')
opt.add_argument('--profile-directory=Default')
該程式將無錯誤地編譯,Chrome 驅動程式將按預期使用用戶資料和組態檔目錄。
該問題似乎與當前將變數添加為函式中x變數的引數的方式有關,即:zoptsubmit_profile_path()
opt.add_argument(fr'--user-data-dir="{y}"')
opt.add_argument(f'--profile-directory={z}')
所以,如果有人能解釋為什么錯了,我將不勝感激?有什么更好的方法呢?
uj5u.com熱心網友回復:
此錯誤訊息...
selenium.common.exceptions.WebDriverException: Message: 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
...意味著ChromeDriver無法啟動/產生新的瀏覽背景關系,即Chrome 瀏覽器會話,原因如下:
- 有ChromeDriver行程的先前實體附加到系統記憶體。
- 你有一個手動打開 谷歌瀏覽器 會話已經在運行。
因為根據RemoveOldDevToolsActivePortFile():
Status RemoveOldDevToolsActivePortFile(const base::FilePath& user_data_dir) {
base::FilePath port_filepath = user_data_dir.Append(kDevToolsActivePort);
// Note that calling DeleteFile on a path that doesn't exist returns True.
if (base::DeleteFile(port_filepath)) {
return Status(kOk);
}
return Status(
kUnknownError,
base::StringPrintf(
"Could not remove old devtools port file. Perhaps the given "
"user-data-dir at %s is still attached to a running %s or "
"Chromium process",
user_data_dir.AsUTF8Unsafe().c_str(), kBrowserShortName));
}
參考
您可以在以下位置找到一些相關的詳細討論:
- chromedriver.exe 運行約 8 個 chrome.exe 實體
- 谷歌瀏覽器多行程(32位)
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/414450.html
標籤:
下一篇:在自己的chrome中打開瀏覽器
