profile path
在提供Chrome驱动程序并单击
user_data
按钮,然后单击
open_browser
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=%2Faccount')
# 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
用户数据
按钮,然后单击
打开浏览器
硒。常见的例外情况。WebDriverException:消息:未知错误:
无法删除旧的devtools端口文件。也许是给定的
用户数据目录
“C:\Users\ResetStoreX\AppData\Local\Google\Chrome\User Data”仍为空
连接到正在运行的铬或铬工艺上
这是非常意外的,因为在测试这个程序的时候,我没有打开任何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
和
z
变量作为
opt
可变的
功能,即:
opt.add_argument(fr'--user-data-dir="{y}"')
opt.add_argument(f'--profile-directory={z}')
所以,如果有人能解释为什么错了,我会非常感激?还有什么更好的方法?