如何使用 Python 和 Selenium Webdriver 在 Chrome 中加載默認配置文件? (How to load default profile in Chrome using Python and the Selenium Webdriver?)


問題描述

如何使用 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 Equinixart_architect)

參考文件

  1. How to load default profile in Chrome using Python and the Selenium Webdriver? (CC BY‑SA 2.5/3.0/4.0)

#linux-mint #selenium #python-3.x #selenium-chromedriver






相關問題

C/C++語言中的Conky (Conky in C/C++ language)

關閉終端不記得我對 nvm/npm 的 .profile 更改 (Closing the terminal doesnt remember my .profile changes for nvm/npm)

在 Linux Mint 中升級 Python 版本 (Upgrade Python version in Linux Mint)

試圖消除 python3 Gtk3 錯誤信息 (Trying to eliminate python3 Gtk3 error message)

在我的 Linux Mint 上安裝 NetBeans 後出現問題,它無法執行任何操作 (i have problem after installing NetBeans on my Linux Mint it can't do anything)

Linux Mint 19.1,白色背景上的白色文本 (Linux Mint 19.1, white text on white background)

PhpStorm 找不到 Linux Mint 系統變量 (PhpStorm doesn't find Linux Mint system variables)

在 Linux Mint 上安裝 Android Studio 的問題 (Problems with Installing Android Studio on Linux Mint)

如何解決 Linux 中的“npm ERR!code ELIFECYCLE”錯誤? (How to solve "npm ERR! code ELIFECYCLE" error in Linux?)

在 Linux Mint 20.1 / Ubuntu 20.04 上安裝/卸載 Docker 後網絡連接斷開 (Broken network connection after Docker install/uninstall on Linux Mint 20.1 / Ubuntu 20.04)

如何使用 Python 和 Selenium Webdriver 在 Chrome 中加載默認配置文件? (How to load default profile in Chrome using Python and the Selenium Webdriver?)

如何使用終端重命名 linux 中的目錄? (How can I rename a directory in linux with terminal?)







留言討論