首页 > 解决方案 > Selenium 在 ubuntu 20.04 上的烧瓶路线内崩溃(chrome 和 firefox)

问题描述

运行 Ubuntu 20.04 LTS 服务器

试图通过烧瓶路线中的硒保存屏幕截图。

问题是无论我尝试什么它都会崩溃。

使用--headless

@api.route('/image/<path:encoded_url>.png')
def generate_image(encoded_url):
    """
    Returns an image (PNG) of a URL. The URL is encoded in the path of the image being requested.
    """
    from selenium import webdriver
    from selenium.webdriver.chrome.options import Options

    options = Options()
    options.headless = True
    options.add_argument("--disable-gpu")
    options.add_argument("--disable-dev-shm-using")

    driver = webdriver.Chrome(f"{os.getcwd()}/chromedriver", options=options)

    url = urllib.parse.unquote_plus(encoded_url)

    driver.get(url if "http" in url else "https://" + url)
    driver.set_window_size(1200, 630)

    while True:
        x = driver.execute_script("return document.readyState")
        if x == "complete":
            break
   

    driver.save_screenshot("screen.png")
    driver.close()

    return send_file("screen.png", mimetype='image/png')

我已经尝试了所有方法,但 firefox 以错误 127 退出(关于此的在线不多)

selenium.common.exceptions.WebDriverException: Message: Process unexpectedly closed with status 127

我试过用 Xvfb 运行但没有运气。

标签: pythonselenium

解决方案


好的,所以在尝试了一大堆选项之后,问题出在我的 gunicorn 设置上。

cd /etc/systemd/system/

打开你为 guinocorn 做的服务

sudo nano {servicename}

问题是环境线

最初我是这样的:

Environment="PATH=/home/ubuntu/retrotex/environment/bin"

但是,一旦我将其更改为以下内容,一切正常:

Environment="PATH=/home/ubuntu/retrotex/environment/bin:/usr/bin:/bin"

似乎烧瓶无权访问运行 chome 或 selenium 所需的文件夹。

确保运行以下命令以重新加载服务

sudo systemctl daemon-reload
sudo systemctl restart {service_name}

真是浪费了一天。


推荐阅读