selenium.common.exceptions.WebDriverException:消息:未知错误:net::ERR_CONNECTION_RESET

Cha*_*ang 5 python gunicorn selenium-webdriver nginx-reverse-proxy ubuntu-18.04

我对这些技术很陌生。
我的操作系统是 ubuntu-18.04,并使用 anaconda 创建一个虚拟环境,并通过 pip 安装和所有必要的软件包。我还安装了相同版本的 Chrome 和 chromedriver 来运行 selenium。我还在/etc/systemd/system下创建了一个test.service

我无法在 stackoverflow 中发布图片。该问题可以在屏幕截图中使用命令 ( )
看到sudo systemctl status test

Gunicorn[25770]:文件“/home/anaconda/port8788/hct/hct_information.py”,第21行,在testDriver中
gunicorn[25770]:driver.get(url)
gunicorn[25770]:文件“/home/chaoyang/anaconda3 /envs/selenium/lib/python3.9/site-packages/selenium/webdriver/remote/webdriver.py”,第 333 行,在 get
Gunicorn[25770] 中:self.execute(Command.GET, {'url': url })
gunicorn[25770]:文件“/home/chaoyang/anaconda3/envs/selenium/lib/python3.9/site-packages/selenium/webdriver/remote/webdriver.py”,第321行,执行
gunicorn[25770] :self.error_handler.check_response(响应)
gunicorn[25770]:文件“/home/chaoyang/anaconda3/envs/selenium/lib/python3.9/site-packages/selenium/webdriver/remote/errorhandler.py”,第242行,在 check_response
Gunicorn[25770] 中:引发异常类(消息、屏幕、堆栈跟踪)gunicorn[25770]:selenium.common.exceptions.WebDriverException:消息:未知错误:net::ERR_CONNECTION_RESET Gunicorn[25770]:(会话信息:无头镀铬=86.0.4240.183)

我的烧瓶代码如下:

import flask
from flask import request
import argparse
from hct import hct_information as hct

app = flask.Flask(__name__)

@app.route("/test")
def test():
    return hct.testDriver('https://www.google.com/')

if __name__ == "__main__":
    app.run(debug=True, host='0.0.0.0')
Run Code Online (Sandbox Code Playgroud)

我的硒代码如下:

from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.keys import Keys
from selenium.common.exceptions import NoSuchElementException
import time

def testDriver(url):
options = Options()
options.binary_location = '/usr/bin/google-chrome'
options.add_argument('--headless')
options.add_argument('--disable-gpu')
options.add_argument('--no-sandbox') #for linux avoid chrome not started
options.add_experimental_option("excludeSwitches", ['enable-automation'])
options.add_argument('--user-agent="Mozilla/5.0 (Windows Phone 10.0; Android 4.2.1; Microsoft; Lumia 640 XL LTE) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/42.0.2311.135 Mobile Safari/537.36 Edge/12.10166"')
#options.add_argument('window-size=500*250')
driver = webdriver.Chrome(executable_path=('/usr/bin/chromedriver'), options=options)
driver.get(url)
time.sleep(1)

title = driver.title
driver.close()
return title
Run Code Online (Sandbox Code Playgroud)

这应该是一个简单的程序。不幸的是,当我使用 python 单独测试来运行 Flask api 时,一切都很好,即使在gunicorn --bind 0.0.0.0:5000 test:app没有上述消息的情况下也可以运行。任何想法我做错了什么???

Zvi*_*Zvi 8

我有一个类似的案例,并发现就我而言,问题是由于使用无头网络选项造成的。

尝试在代码中注释该行,看看错误是否消失。

我仍在研究如何让应用程序运行而不显示网页并且不出现错误。

  • 您是否找到了一种在不显示网页的情况下运行并且不会收到错误的方法? (6认同)