gpt4 book ai didi

python 使用 selenium,错误 : chrome unexpectedly exited. 状态代码为:0

转载 作者:行者123 更新时间:2023-12-05 08:05:53 24 4
gpt4 key购买 nike

不是的转发

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,以便我可以捕获现有 session 。

失败的代码:

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)

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

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

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

谷歌浏览器信息:

enter image description here

最佳答案

拇指法则

A common cause for Chrome to crash during startup is running Chrome as root user (administrator) on Linux. While it is possible to work around this issue by passing --no-sandbox flag when creating your WebDriver session, such a configuration is unsupported and highly discouraged. You need to configure your environment to run Chrome as a regular user instead.


这个错误信息...

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 浏览器 session 。

根据您的代码试验,您似乎正在尝试访问 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")

引用资料

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

关于python 使用 selenium,错误 : chrome unexpectedly exited. 状态代码为:0,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63228230/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com