經過今天非常詳盡的研究,我設法理解并制作了一個簡單的程式,該程式將使用該selenium模塊打開一個帶有我的用戶資料和默認組態檔的 chrome 視窗,這里:
import tkinter as tk
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() #selenium options
opt.add_argument("--user-data-dir=" r"C:\Users\ResetStoreX\AppData\Local\Google\Chrome\User Data\Default") #PATH profile
opt.add_argument('--profile-directory=Default') #Profile to use
s = Service('C:\Program Files\Google\Chrome\Application\chrome.exe')
def open_chrome_profile():
driver = webdriver.Chrome(service=s, options=opt)
driver.get('https://opensea.io/asset/create')
#####BUTTON ZONE#######
open_browser = tk.Button(root, width=20, text="Open OpenSea Tab", command=open_chrome_profile) #executes the function when clicked
open_browser.grid(row=22, column=1)
#####BUTTON ZONE END#######
root.mainloop()
問題是它根本沒有按預期作業:
首先,單擊Open OpenSea Tab按鈕后,程式會打開一個帶有我的用戶資料和默認組態檔的 chrome 視窗,但它不會轉到提供的鏈接 ( 'https://opensea.io/asset/create')。
其次,它還會在不引發任何錯誤的情況下使 GUI 崩潰,更糟糕的是,它也會使 Spyder 內核崩潰!

我很困惑,對我來說,我似乎沒有在這里做任何非法的事情,但如果有人能解釋我應該如何修補這個問題,我將不勝感激?
我設法按照其他問題的建議構建了上面的代碼,但我沒想到最終會遇到像這樣的奇怪問題......
uj5u.com熱心網友回復:
您需要在這里處理幾件事:
您需要
Default從組態檔 PATH 中洗掉。因此,您的代碼行將是:opt.add_argument("--user-data-dir=r'C:\Users\ResetStoreX\AppData\Local\Google\Chrome\User Data') #PATH profile
您可以在以下位置找到一些相關的詳細討論:
- 如何在 Selenium Webdriver Python 3 中使用 Chrome 組態檔
- selenium.common.exceptions.WebDriverException:訊息:未知錯誤:Chrome 無法啟動:在 Python 中使用 ChromeDriver 和 Selenium 崩潰
- Selenium:指向默認的 Chrome 會話
您需要傳遞ChromeDriver的絕對路徑,
Service()如下所示:s = Service('C:\path\to\chromedriver.exe')
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/411721.html
標籤:
