[Python] Selenium won't Start headless on remote server

Discussão em 'Python' iniciado por Stack, Junho 26, 2025.

  1. Stack

    Stack Membro Participativo

    I try to automate a routine task on web with Selenium on remote Server.

    So when I try to start webdriver in python it fails with connection refused error:

    instance-1@instance-1:~$ python
    Python 2.7.17 (default, Apr 15 2020, 17:20:14)
    [GCC 7.5.0] on linux2
    Type "help", "copyright", "credits" or "license" for more information.
    >>> from selenium import webdriver
    >>> from selenium.webdriver.firefox.options import Options
    >>> options = Options()
    >>> options.headless = True
    >>> options.log.level = "trace"
    >>> driver = webdriver.Firefox(options=options )


    Traceback (most recent call last):
    File "<stdin>", line 1, in <module>
    File "/home/instance1/.local/lib/python2.7/site-packages/selenium/webdriver/firefox/webdriver.py", line 174, in __init__
    keep_alive=True)
    File "/home/instance-1/.local/lib/python2.7/site-packages/selenium/webdriver/remote/webdriver.py", line 157, in __init__
    self.start_session(capabilities, browser_profile)
    File "/home/instance-1/.local/lib/python2.7/site-packages/selenium/webdriver/remote/webdriver.py", line 252, in start_session
    response = self.execute(Command.NEW_SESSION, parameters)
    File "/home/instance-1/.local/lib/python2.7/site-packages/selenium/webdriver/remote/webdriver.py", line 321, in execute
    self.error_handler.check_response(response)
    File "/home/instance-1/.local/lib/python2.7/site-packages/selenium/webdriver/remote/errorhandler.py", line 242, in check_response
    raise exception_class(message, screen, stacktrace)
    selenium.common.exceptions.TimeoutException: Message: connection refused


    I use Python 2.7 and Firefox, detailed versions are below.

    instance-1@instance-1:~$
    instance-1@instance-1:~$ which firefox
    /usr/bin/firefox
    instance-1@instance-1:~$ firefox --version
    Mozilla Firefox 75.0
    instance-1@instance-1:~$ python -c "import selenium; print(selenium.__version__)"
    3.141.0
    instance-1@instance-1:~$ geckodriver --version
    geckodriver 0.26.0 (e9783a644016 2019-10-10 13:38 +0000)

    The source code of this program is available from
    testing/geckodriver in https://hg.mozilla.org/mozilla-central.

    This program is subject to the terms of the Mozilla Public License 2.0.
    You can obtain a copy of the license at https://mozilla.org/MPL/2.0/.
    instance-1@instance-1:~$ which geckodriver
    /usr/local/bin/geckodriver
    instance-1@instance-1:~$


    Also, I've setted virtual display via Xvbf like this

    instance-1@instance-1:~$ Xvfb :99 -ac &
    [1] 16171
    instance-1@instance-1:~$ export DISPLAY=:99


    My geckotrace output is

    1588545824498 mozrunner::runner INFO Running command: "/usr/bin/firefox" "-marionette" "-headless" "-foreground" "-no-remote" "-profile" "/tmp/rust_mozprofileOn8vbe"
    1588545824519 geckodriver::marionette DEBUG Waiting 60s to connect to browser on 127.0.0.1:44835
    *** You are running in headless mode.
    1588545828833 addons.webextension.doh-rollout@mozilla.org WARN Loading extension 'doh-rollout@mozilla.org': Reading manifest: Invalid extension permission: networkStatus
    1588545829585 addons.webextension.screenshots@mozilla.org WARN Loading extension 'screenshots@mozilla.org': Reading manifest: Invalid extension permission: mozillaAddons
    1588545829585 addons.webextension.screenshots@mozilla.org WARN Loading extension 'screenshots@mozilla.org': Reading manifest: Invalid extension permission: telemetry
    1588545829585 addons.webextension.screenshots@mozilla.org WARN Loading extension 'screenshots@mozilla.org': Reading manifest: Invalid extension permission: resource://pdf.js/
    1588545829585 addons.webextension.screenshots@mozilla.org WARN Loading extension 'screenshots@mozilla.org': Reading manifest: Invalid extension permission: about:reader*
    1588545830466 Marionette TRACE Marionette enabled
    1588545831067 Marionette TRACE Received observer notification toplevel-window-ready
    1588545884800 mozrunner::runner DEBUG Killing process 16183
    Exiting due to channel error.
    Exiting due to channel error.
    1588545885811 webdriver::server DEBUG <- 500 Internal Server Error {"value":{"error":"timeout","message":"connection refused","stacktrace":""}}


    Should I specify something else?

    Continue reading...

Compartilhe esta Página