【问题标题】:python using selenium, error: chrome unexpectedly exited. Status code was: 0python 使用 selenium,错误:chrome 意外退出。状态码是:0
【发布时间】:2020-11-23 10:46:21
【问题描述】:

不是转帖

selenium.common.exceptions.WebDriverException: Message: unknown error: Chrome failed to start: crashed with ChromeDriver and Selenium in Python

我正在使用 Linux,创建新配置文件不是一种选择。我想加载一个现有配置文件(而不是创建一个新配置文件),就像 selenium gui 一样。

我可以让 chromium 正常工作,但不能让 google chrome 正常工作。 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)

有效但启动 chromium 而不是 google-chrome 的代码:

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)

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

我很确定我安装了正确的 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

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

谷歌浏览器信息:

【问题讨论】:

    标签: python selenium google-chrome selenium-webdriver selenium-chromedriver


    【解决方案1】:

    拇指规则

    Chrome 在启动期间崩溃的一个常见原因是在 Linux 上以 root 用户 (administrator) 运行 Chrome。虽然可以通过在创建 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")
      

    参考文献

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

    【讨论】:

    • 我不是以 root 身份启动的。我的缓存路径应该已经解决了这个问题。
    • 抱歉,我说得太早了。同样的问题。
    猜你喜欢
    • 2018-11-27
    • 2017-04-21
    • 2017-01-11
    • 2020-09-18
    • 1970-01-01
    • 2019-05-29
    • 2018-11-23
    • 2019-02-02
    • 2017-02-27
    相关资源
    最近更新 更多