【问题标题】:How to load default profile in Chrome using Python and the Selenium Webdriver?如何使用 Python 和 Selenium Webdriver 在 Chrome 中加载默认配置文件?
【发布时间】:2021-05-17 21:13:37
【问题描述】:

希望你们一切都好 =) 这是我在 stackoverflow 上的第一篇文章/问题 =)

在尝试了thread 中的所有答案后,我正在写这篇文章。 (我尝试的最后一个答案来自 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/")

【问题讨论】:

    标签: python-3.x selenium selenium-chromedriver linux-mint


    【解决方案1】:

    我没有相同的路径,但是您可以确定要使用的确切二进制文件,铬或铬

    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/")
    

    【讨论】:

    • 感谢@art_architect。设法让它打开 Chromium,但它仍然没有检索到正确的配置文件。这是我转到 chrome://version/ 可执行路径 /usr/lib/chromium/chromium 配置文件路径 /tmp/.com.google.Chrome.YM3zZE/Default 时得到的
    • 命令行.... /usr/lib/chromium/chromium --disable-background-networking --disable-client-side-phishing-detection --disable-default-apps --disable -hang-monitor --disable-popup-blocking --disable-prompt-on-repost --disable-sync --enable-automation --enable-blink-features=ShadowDOMV0 --enable-logging --log-level= 0 --no-first-run --no-service-autorun --password-store=basic --remote-debugging-port=0 --test-type=webdriver --use-mock-keychain --user-data -dir=/tmp/.com.google.Chrome.YM3zZE --user-data-dir = ./home/equinix/.config/chromium/Default --flag-switches-begin --flag-switches-end 数据: ,
    • 我不知道为什么配置文件指向帽子位置。您是否通过 sudo apt-get install chromium 安装了 chromium?还是存档提取?
    • 感谢您抽出宝贵时间@art_architect 来看看这个。我通过 sudo apt install chromium ... 安装了它。开始这个“项目”时,我从来没有想过在这个脚本的开头被卡住这么久。大声笑
    • 当我添加 *** ch_options.add_argument("profile-directory=Monkey") *** 我得到 *** Profile Path /tmp/.com.google.Chrome.6xxT6e/Monkey * ** 在配置文件路径中(在 chrome://version 中。所以它似乎正在读取该参数,但不是将其指向 user-data-dir
    猜你喜欢
    • 2015-09-12
    • 1970-01-01
    • 1970-01-01
    • 2019-12-01
    • 2014-11-04
    • 1970-01-01
    • 2013-01-06
    • 2019-02-22
    相关资源
    最近更新 更多