python - 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 运行但没有运气。
解决方案
好的,所以在尝试了一大堆选项之后,问题出在我的 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}
真是浪费了一天。
推荐阅读
- r - R中的有向图上的随机游走
- assembly - X.86 X64 汇编器中的正确堆栈操作
- java - 从 Azure IoT 向 esp8266 发送命令
- sql - 如果列已经自己建立索引,那么创建多列索引是否有帮助?
- linux - 提高使用 ffmpeg 制作幻灯片视频的性能
- pyinstaller - pyinstaller ImportError: C extension: No module named np_datetime not built
- excel-2010 - Excel:参数太多
- database - How Do I Apply TF-IDF When I Only Have a Subset of the Total Documents?
- r - 通过匹配作为列添加的因子匹配因子级别的级别来合并两个数据框
- actionscript-3 - 从另一个 MC 内部控制源 MC 内部的帧导航