代码之家  ›  专栏  ›  技术社区  ›  NoahVerner

如果我正确地更新了函数中的selenium Options(),为什么这个python程序中会出现“WebDriverException:Message:unknown error”?

  •  0
  • NoahVerner  · 技术社区  · 4 年前

    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}')
    

    所以,如果有人能解释为什么错了,我会非常感激?还有什么更好的方法?

    1 回复  |  直到 4 年前
        1
  •  1
  •   undetected Selenium    4 年前

    此错误消息。。。

    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 无法启动/生成新的 浏览上下文 由于下列原因之一而进行的会话:

    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));
    }
    

    工具书类