selenium.common.exceptions.WebDriverException: Message: unknown error: net::ERR_CONNECTION_RESET

Viewed 6555

I am new to those technologies.
My OS is ubuntu-18.04 and using anaconda to create a virtual environment with pip installation with all necessary package. I also install Chrome and chromedriver with the same version to run the selenium. I also create a test.service under /etc/systemd/system

I cann't post image in stackoverflow.
The problem can be seen in screenshot with command (sudo systemctl status test)

gunicorn[25770]: File "/home/anaconda/port8788/hct/hct_information.py", line 21, in testDriver
gunicorn[25770]: driver.get(url)
gunicorn[25770]: File "/home/chaoyang/anaconda3/envs/selenium/lib/python3.9/site-packages/selenium/webdriver/remote/webdriver.py", line 333, in get
gunicorn[25770]: self.execute(Command.GET, {'url': url})
gunicorn[25770]: File "/home/chaoyang/anaconda3/envs/selenium/lib/python3.9/site-packages/selenium/webdriver/remote/webdriver.py", line 321, in execute
gunicorn[25770]: self.error_handler.check_response(response)
gunicorn[25770]: File "/home/chaoyang/anaconda3/envs/selenium/lib/python3.9/site-packages/selenium/webdriver/remote/errorhandler.py", line 242, in check_response
gunicorn[25770]: raise exception_class(message, screen, stacktrace) gunicorn[25770]: selenium.common.exceptions.WebDriverException: Message: unknown error: net::ERR_CONNECTION_RESET gunicorn[25770]: (Session info: headless chrome=86.0.4240.183)

My Flask Code is below:

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')

My selenium Code is below:

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

It should be a simple program. Unfortunately, when I test separately using python to run the flask api everything is fine, even with gunicorn --bind 0.0.0.0:5000 test:app is also run without above message. Any Idea where I done things incorrectly????

1 Answers

I've got a similar case and found out that the problem, in my case, was due to using the option of headless web.

Try commenting that line in your code and see if the error is gone.

I am still investigation how to have the app run without displaying the web and not getting the error.

Related