Selenium in Django on Linux - Ubuntu

I have problem with python selenium in Django when my project run on apache2 - Ubuntu Desktop. My app send forms to selenium script which reset user password. If im running server like this python3 manage.py runserver everything is fine and works good. When app works on apache i got error like this:

Exception Type: TimeoutException Exception Value: Message: Failed to read marionette port

Im sending forms - "name" and "ID" from function 'submitmyfrom' to 'reset_user_pw'.

view.py:

def submitmyfrom(request):
    form = FormsReset(request.POST)

    my_name = request.POST['name']
    my_id = request.POST['id']
    ip = request.META.get('REMOTE_ADDR')


    if len(id) < 12:

            passwd = reset_user_pw(user_name=my_name)
            mydictionary = {
            "my_name" : my_name,
            "my_id" : my_id,
            'password' : passwd

            }
            return render(request,'submitmyfrom.html', context=mydictionary)

reset_user_pw:

def reset_user_pw(user_name):
    os.environ['MOZ_HEADLESS'] = '1'
    pwd = mypwd
    LoginName = "login" 
    user_login = user_name

    cert = webdriver.FirefoxProfile()
    cert.accept_untrusted_certs = True
    web = webdriver.Firefox(executable_path='/var/www/my_project/src/geckodriver',                  
    log_path='/var/www/my_project/src/Reset/geckodriver.log',   
    service_log_path='/var/www/my_project/src/myapp/geckodriver.log')
    web.get('https://example.com/test')
    time.sleep(1)

next is the rest of the function reset_user_pw I use firefox and would ideally like to stay with it

What can i do to make it on apache2. I remind you that the python3 manage.py runserver run fine

The issue you're encountering with Selenium in Django when running the app on Apache2 is due to the environment variable MOZ_HEADLESS not being set properly.

To resolve this issue, you need to make sure that the environment variable MOZ_HEADLESS is set before running the script. One way to do this is to set the environment variable in the Apache configuration file, which is usually located at /etc/apache2/envvars.

Add the following line to the file:

export MOZ_HEADLESS=1

Then, restart Apache to apply the changes:

sudo systemctl restart apache2

This should resolve the issue and allow the script to run correctly when the app is run on Apache2.

Back to Top