Chrome无法启动:crashed.(chrome not reachable)(从chrome [..]启动的进程不再运行,所以[..] Chrome已经崩溃,)

juud5qan  于 12个月前  发布在  Go
关注(0)|答案(1)|浏览(282)

我有一个使用Selenium运行Python脚本的Flask应用程序,它接收带有Selenium应该打开的URL的POST请求。Python脚本托管在Google Cloud中的VM上,当我使用Flask运行代码时,它工作得很好,发送到服务器的POST请求也得到了很好的解析。一切都按预期和预期进行。
我一直在尝试使用Nginx,Gunicorn和systemd来部署它,以便24/7正常运行,但每当POST请求传递到我的服务器时,我在logs.log文件中得到以下错误:

selenium.common.exceptions.WebDriverException: Message: unknown error: Chrome failed to start: crashed.
  (chrome not reachable)
  (The process started from chrome location /usr/bin/google-chrome is no longer running, so ChromeDriver is assuming that Chrome has crashed.)

我已经看过StackOverflow上关于这个问题的所有其他线程,所有这些线程似乎都源于chromedriver配置问题或Chrome和chromeDriver的不兼容版本,但由于它在Flask上运行完全正常,但只有在我通过Nginx和Gunicorn路由它时才会中断,我高度怀疑情况是否如此。我已经检查了我的Chrome和chromeDriver版本,它们似乎都很好(而且,一般来说,我相当肯定我的所有路径都在我的代码中的其他地方):

chromedriver --version
ChromeDriver 115.0.5790.170

google-chrome --version
Google Chrome 115.0.5790.170 

selenium
Version: 4.11.2

因此,作为参考,以下是我在Python文件中的配置:

from flask import Flask, request, jsonify
from flask_cors import CORS

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.common.exceptions import NoSuchElementException
from selenium.webdriver.common.by import By
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.chrome.options import Options

import time
import logging

logging.basicConfig(filename='/home/username/logs.log', level=logging.INFO)

app = Flask(__name__)
CORS(app)  

def open_url_in_selenium(url):
    service = Service('/usr/local/bin/chromedriver')
    service.start()
    options = webdriver.ChromeOptions()
    options.add_argument('--no-sandbox') 
    options.add_argument('--disable-dev-shm-usage') 
    options.add_argument('--headless')
    options.add_argument('--disable-gpu')
    options.add_argument('--disable-extensions')
    options.add_argument('--remote-debugging-port=9222')
    options.add_argument('--log-level=DEBUG')
    options.add_argument('--enable-logging')
    options.add_argument('--disable-software-rasterizer')
    options.binary_location = '/usr/bin/google-chrome'  
    options.add_argument('--log-path=' + log_file_path)

    driver = webdriver.Chrome(options=options)
    driver.get(url)

    return driver

下面是systemd服务单元(如果相关):

[Unit]
Description=XYZ thing
After=network.target

[Service]
User=username
Group=username
WorkingDirectory=/home/username
ExecStart=/usr/bin/gunicorn --workers 1  --bind X.X.X.X:XXXX server:app
Restart=always

[Install]
WantedBy=multi-user.target

还有最后一件事要注意:出于某种原因,即使我在Flask上运行代码时,我也需要使用sudo python3 filename.py才能使代码在没有拒绝权限的情况下运行。这可能是权限问题吗?当我运行systemd时,我也使用sudo,所以我认为它会很好,但也许不是?以下是我一直在使用的命令:

sudo systemctl start service-unit-name
sudo systemctl enable service-unit-name
sudo systemctl stop service-unit-name
sudo systemctl restart service-unit-name

如果这是问题所在,有人能解释如何正确配置权限吗?有没有人知道发生了什么事?我已经想了好几个小时了!非常感谢

olmpazwi

olmpazwi1#

由于您使用的是最新的selenium(v4.11.2),因此实际上不必传递chromedriverbrowser的位置。Selenium可以为您下载和管理驱动程序。
尝试删除下面的行,并让selenium以编程方式下载chromedriver

service = Service('/usr/local/bin/chromedriver')
service.start()

options.binary_location = '/usr/bin/google-chrome'

即使在尝试上述操作后,如果无法启动浏览器,则可以尝试以下操作:
转到以下路径usr/local/bin/chromedriver,并删除所有旧的驱动程序(除了v115),如果您看到。
引用

相关问题