3

不是转发

selenium.common.exceptions.WebDriverException:消息:未知错误:Chrome 无法启动:在 Python 中使用 ChromeDriver 和 Selenium 崩溃

我正在使用 Linux 并且不能创建新的配置文件。我想像selenium gui 一样加载现有的配置文件(而不是创建新的配置文件)。

我能够让铬发挥作用,但不能让谷歌浏览器发挥作用。Chrome 会打开,但会返回

selenium.common.exceptions.WebDriverException: Message: Service /opt/google/chrome/chrome unexpectedly exited. Status code was: 0

错误。

我正在尝试使用用户目录访问权限启动 google chrome,以便捕获现有会话。

失败的代码:

option.add_argument("user-data-dir=/home/user/.config/google-chrome/Default/") #)PATH is path to your chrome profile
driver = webdriver.Chrome('/opt/google/chrome/chrome', options=option)

有效但启动铬而不是谷歌铬的代码:

option.add_argument("user-data-dir=/home/user/snap/chromium/common/.cache/chromium/Default/") #)PATH is path to your>
driver = webdriver.Chrome('/snap/bin/chromium.chromedriver', options=option)

我确定我使用的是正确的可执行文件

htop

我很确定我安装了正确的 chromedriver 驱动程序

root@Inspiron-laptop:/home/user# pip3 install chromedriver-autoinstaller
Requirement already satisfied: chromedriver-autoinstaller in /usr/local/lib/python3.8/dist-packages (0.2.2)

只是使用不当。

如何在访问缓存目录时从 selenium 中启动 google-chrome?

我在 Ubuntu 20.04

更新:

完整脚本:

#!/usr/bin/python3

from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
from seleniumbase import BaseCase
from selenium.webdriver.chrome.options import Options
import time
import random

minptime = 25
maxptime = 120

class MyweClass(BaseCase):
        def method_a():
                option = webdriver.ChromeOptions()
                option.add_argument('--disable-notifications')
                option.add_argument("--mute-audio")
                option.add_argument("user-data-dir=/home/user/.config/google-chrome/Default/") #)PATH is path to your chrome profile
                driver = webdriver.Chrome('/opt/google/chrome/chrome', options=option)
                driver.get("https://world.com/myworld")
                print(f'driver.command_executor._url: {driver.command_executor._url}')
                print(f'driver.session_id: {driver.session_id}')
                time.sleep(18)
                return driver
driver = MyweClass.method_a()

更新二:

使用相同的错误

option.add_argument("user-data-dir=~/.config/google-chrome/Default/")

driver = webdriver.Chrome('/opt/google/chrome/google-chrome', options=option)

chmod -R 777 /home/user/.config

确保用户以用户身份访问缓存目录。

谷歌浏览器信息:

在此处输入图像描述

4

1 回答 1

-2

拇指规则

Chrome 在启动期间崩溃的一个常见原因是在 Linux 上以root用户 ( ) 身份运行 Chrome。administrator虽然可以通过在创建 WebDriver 会话时传递标志来解决此问题--no-sandbox,但这种配置不受支持且不鼓励使用。您需要将环境配置为以普通用户身份运行 Chrome。


此错误消息...

selenium.common.exceptions.WebDriverException: Message: unknown error: Chrome failed to start: exited abnormally
  (Driver info: chromedriver=2.26.436382 (70eb799287ce4c2208441fc057053a5b07ceabac),platform=Linux 4.15.0-109-generic x86_64)

...意味着ChromeDriver无法启动/产生新的浏览上下文,即Chrome 浏览器会话。

根据您的代码试验,您似乎正在尝试访问Chrome 配置文件,因此您可以使用以下解决方案:

  • 代码块:

    from selenium import webdriver
    from selenium.webdriver.chrome.options import Options
    
    options = Options()
    options.add_argument("user-data-dir=C:\\path\\to\\your\\profile\\Google\\Chrome\\User Data\\Profile 2")
    driver = webdriver.Chrome(executable_path=r'C:\path\to\chromedriver.exe', chrome_options=options)
    driver.get("https://www.google.co.in")
    

参考

您可以在以下位置找到一些详细的讨论:

于 2020-08-03T13:43:43.817 回答