[Python] Firefox Selenium WebDriver Timeout in OpenShift Pod

Discussão em 'Python' iniciado por Stack, Abril 15, 2025.

  1. Stack

    Stack Membro Participativo

    I'm experiencing a timeout issue when trying to run Selenium with Firefox in an OpenShift pod. I'm using Python 3.9 with Selenium to automate browser interactions with a web application.

    When executing my Python script that uses Selenium with Firefox in headless mode, I consistently get a timeout error during the WebDriver initialization phase. The connection to the geckodriver process times out after the default wait period (120 seconds).

    My Code:

    from selenium.webdriver.firefox.options import Options as FirefoxOptions
    from selenium.webdriver.common.by import By
    from selenium.webdriver.support import expected_conditions as EC
    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.common.keys import Keys
    from selenium import webdriver
    from selenium.webdriver.firefox.service import Service
    import time
    import urllib3
    import logging
    import os

    # Init
    urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)
    logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')

    firefox_options = FirefoxOptions()
    firefox_options.add_argument("--headless")
    firefox_options.add_argument("--no-sandbox")
    firefox_options.add_argument("--disable-dev-shm-usage")
    firefox_options.add_argument("--disable-gpu")

    #geckodriver doit être installé et disponible dans le PATH
    gecko_path = os.environ.get("GECKO_DRIVER_PATH", "/usr/local/bin/geckodriver")
    service = Service(executable_path=gecko_path)

    driver = webdriver.Firefox(options=firefox_options, service=service)

    def connect_to_cacti():
    try:
    #proxy OpenShift
    proxy_env = os.environ.get("HTTP_PROXY")
    if proxy_env:
    logging.info(f"Utilisation du proxy: {proxy_env}")
    firefox_options.set_preference("network.proxy.type", 1)
    proxy_components = proxy_env.replace("http://", "").split(":")
    firefox_options.set_preference("network.proxy.http", proxy_components[0])
    firefox_options.set_preference("network.proxy.http_port", int(proxy_components[1]))
    firefox_options.set_preference("network.proxy.ssl", proxy_components[0])
    firefox_options.set_preference("network.proxy.ssl_port", int(proxy_components[1]))

    driver.get('https://cacti-sma-ocb.securite.fr.intraorange/cacti/index.php')

    wait = WebDriverWait(driver, 20)

    login_field = wait.until(
    EC.presence_of_element_located((By.NAME, "login_username"))
    )
    password_field = wait.until(
    EC.presence_of_element_located((By.NAME, "login_password"))
    )

    login_field.send_keys('*******')
    password_field.send_keys('********')

    password_field.send_keys(Keys.RETURN)
    time.sleep(5)

    except Exception as e:
    logging.error(f"Une erreur s'est produite lors de la connexion à Cacti: {str(e)}")
    raise

    finally:
    logging.debug("Tentative de connexion à Cacti terminée")

    def get_widget(url):
    try:
    driver.get(url)
    # Augmenter le temps d'attente pour s'assurer que la page se charge complètement
    time.sleep(5)
    driver.save_screenshot("photo.png")
    logging.info("Capture d'écran prise avec succès")
    except Exception as e:
    logging.error(f"Erreur lors de la capture du widget: {str(e)}")
    raise
    finally:
    True

    def cleanup():
    if driver:
    driver.quit()

    # Main
    if __name__ == "__main__":
    try:
    connect_to_cacti()
    # Exemple d'appel à get_widget
    # get_widget('http://cacti-sma-ocb.securite.fr.intraorange/cacti/graph.php?action=zoom&local_graph_id=1533938&rra_id=0&view_type=tree&graph_start=1733953560&graph_end=1741729560')
    finally:
    cleanup()



    Error Message

    Traceback (most recent call last):
    File "/usr/local/lib/python3.9/site-packages/urllib3/connectionpool.py", line 468, in _make_request
    six.raise_from(e, None)
    File "<string>", line 3, in raise_from
    File "/usr/local/lib/python3.9/site-packages/urllib3/connectionpool.py", line 463, in _make_request
    httplib_response = conn.getresponse()
    File "/usr/local/lib/python3.9/http/client.py", line 1377, in getresponse
    response.begin()
    File "/usr/local/lib/python3.9/http/client.py", line 320, in begin
    version, status, reason = self._read_status()
    File "/usr/local/lib/python3.9/http/client.py", line 281, in _read_status
    line = str(self.fp.readline(_MAXLINE + 1), "iso-8859-1")
    File "/usr/local/lib/python3.9/socket.py", line 716, in readinto
    return self._sock.recv_into(b)
    socket.timeout: timed out

    During handling of the above exception, another exception occurred:

    Traceback (most recent call last):
    File "/app/seleniumCapture.py", line 28, in <module>
    driver = webdriver.Firefox(options=firefox_options, service=service)
    File "/usr/local/lib/python3.9/site-packages/selenium/webdriver/firefox/webdriver.py", line 71, in __init__
    super().__init__(command_executor=executor, options=options)
    File "/usr/local/lib/python3.9/site-packages/selenium/webdriver/remote/webdriver.py", line 250, in __init__
    self.start_session(capabilities)
    File "/usr/local/lib/python3.9/site-packages/selenium/webdriver/remote/webdriver.py", line 342, in start_session
    response = self.execute(Command.NEW_SESSION, caps)["value"]
    File "/usr/local/lib/python3.9/site-packages/selenium/webdriver/remote/webdriver.py", line 427, in execute
    response = self.command_executor.execute(driver_command, params)
    File "/usr/local/lib/python3.9/site-packages/selenium/webdriver/remote/remote_connection.py", line 404, in execute
    return self._request(command_info[0], url, body=data)
    File "/usr/local/lib/python3.9/site-packages/selenium/webdriver/remote/remote_connection.py", line 428, in _request
    response = self._conn.request(method, url, body=body, headers=headers, timeout=self._client_config.timeout)
    File "/usr/local/lib/python3.9/site-packages/urllib3/request.py", line 81, in request
    return self.request_encode_body(
    File "/usr/local/lib/python3.9/site-packages/urllib3/request.py", line 173, in request_encode_body
    return self.urlopen(method, url, **extra_kw)
    File "/usr/local/lib/python3.9/site-packages/urllib3/poolmanager.py", line 376, in urlopen
    response = conn.urlopen(method, u.request_uri, **kw)
    File "/usr/local/lib/python3.9/site-packages/urllib3/connectionpool.py", line 802, in urlopen
    retries = retries.increment(
    File "/usr/local/lib/python3.9/site-packages/urllib3/util/retry.py", line 552, in increment
    raise six.reraise(type(error), error, _stacktrace)
    File "/usr/local/lib/python3.9/site-packages/urllib3/packages/six.py", line 770, in reraise
    raise value
    File "/usr/local/lib/python3.9/site-packages/urllib3/connectionpool.py", line 716, in urlopen
    httplib_response = self._make_request(
    File "/usr/local/lib/python3.9/site-packages/urllib3/connectionpool.py", line 470, in _make_request
    self._raise_timeout(err=e, url=url, timeout_value=read_timeout)
    File "/usr/local/lib/python3.9/site-packages/urllib3/connectionpool.py", line 358, in _raise_timeout
    raise ReadTimeoutError(
    urllib3.exceptions.ReadTimeoutError: HTTPConnectionPool(host='localhost', port=50597): Read timed out. (read timeout=120)


    Environment Details:

    OS: Running in an OpenShift pod Python version: 3.9 Selenium version: Latest Browser: Firefox in headless mode Proxy: Environment uses a corporate proxy (HTTP_PROXY environment variable set)

    What I've Tried: I've included appropriate Firefox options for containerized environments (--no-sandbox, --disable-dev-shm-usage, --disable-gpu) I've verified that geckodriver is installed at the specified path The proxy configuration appears to be correct for other network operations

    Additional Context: The script is intended to: Connect to a web application Authenticate using credentials Navigate to specific pages Take screenshots The error occurs at the very beginning when trying to initialize the WebDriver, before any navigation attempts.

    Question: What could be causing this timeout issue when running Firefox with Selenium in an OpenShift container, and how can I resolve it?

    Many thanks

    Continue reading...

Compartilhe esta Página