問題描述
如何使用 Python 和 Selenium Webdriver 在 Chrome 中加載默認配置文件? (How to load default profile in Chrome using Python and the Selenium Webdriver?)
希望你們一切都好=)這是我在stackoverflow上的第一篇文章/問題=)
我在嘗試了這個線程。(我嘗試的最後一個答案來自 Youssof H.)
我是一個 python 新手,正在嘗試編寫一個腳本來幫助我將產品上傳到網站。由於我需要登錄才能添加產品,我想為什麼不使用我已經登錄的瀏覽器配置文件,而不是編寫代碼來完成此操作(我認為使用瀏覽器配置文件會更容易)
我已經嘗試了好幾個小時,但我似乎無法自己解決這個問題。
當我運行代碼時,它沒有打開 Chromium,而是一直打開 google‑chrome。在嘗試使用 chromium 之前,我嘗試使用 chrome,如果我打開 chrome,它會打開 google‑chrome‑stable,但是當我運行 python 文件時,它會運行 google‑chrome。
我的操作系統是 Mint 20.1 (Cinnamon),我使用 Visual Studio Code
如果有人能幫我解決這個問題,我將不勝感激 =)
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
# Do not use this path that is extracted from "chrome://version/"
exec_path_chrome = "/usr/bin/chromium"
exec_path_driver = "/home/equinix/drivers/chromedriver"
ch_options = Options() # Chrome Options
# Extract this path from "chrome://version/"
ch_options.add_argument(
"user‑data‑dir = /home/equinix/.config/chromium/Default")
# Chrome_Options is deprecated. So we use options instead.
driver = webdriver.Chrome(executable_path=exec_path_driver, options=ch_options)
driver.get("https://duckduckgo.com/")
參考解法
方法 1:
I dont have the same paths but, you can pinpoint the exact binary that you want to use, either chromium or chrome
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
# Do not use this path that is extracted from "chrome://version/"
exec_path_chrome = "/usr/lib/bin/chromium"
exec_path_driver = "/home/art/drivers/chromedriver"
ch_options = Options() # Chrome Options
# Extract this path from "chrome://version/"
ch_options.add_argument(
"user‑data‑dir = /home/art/.config/chromium/Default")
# in your case its "/usr/lib/bin/chromium/chromium"
ch_options.binary_location = '/usr/lib/chromium/chromium'
#to point to google‑chrome‑stable
#ch_options.binary_location = '/opt/google/chrome/google‑chrome'
# Chrome_Options is deprecated. So we use options instead.
driver = webdriver.Chrome(executable_path=exec_path_driver, options=ch_options)
driver.get("https://duckduckgo.com/")
(by Equinix、art_architect)