0

I'm running Selenium tests using Python and Chrome WebDriver on Windows 10. The setup was working perfectly a couple of days ago, but now I'm getting a ConnectionRefusedError, where it everytime tries to connect to any random port, only not 8080. Here are the details:

Environment:

  • Windows 10
  • PyCharm 2023
  • Chrome browser version: 126
  • ChromeDriver version: 126
  • ChromeDriver is added to the system PATH
  • Python 3.10
  • Selenium 4.22.0

Code:

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.chrome.service import Service as ChromeService
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from webdriver_manager.chrome import ChromeDriverManager
import logging

# Настройка логирования
logging.basicConfig(level=logging.DEBUG)

# Настройка опций для headless режима
chrome_options = webdriver.ChromeOptions()
chrome_options.add_argument("--headless")
chrome_options.add_argument("--disable-gpu")
chrome_options.add_argument("--window-size=1920x1080")

# Инициализация веб-драйвера с опциями
service = ChromeService(executable_path=ChromeDriverManager().install())
driver = webdriver.Chrome(service=service, options=chrome_options)

# Открытие главной страницы
driver.get("http://localhost:8080/")

# Тест 1: Проверка наличия логотипа

def test_logo_presence():
    try:
        # Увеличение времени ожидания до 20 секунд
        logo = WebDriverWait(driver, 20).until(
            EC.presence_of_element_located((By.CSS_SELECTOR, "#logo a"))
        )
        assert logo is not None, "Logo is not present"
        print("Test 1: Logo is present")
    except Exception as e:
        print(f"An error occurred: {e}")
    finally:
        driver.quit()

# Выполнение тестов
test_logo_presence()

Error:

if new_retry.is_exhausted():
    reason = error or ResponseError(cause)
>   raise MaxRetryError(_pool, url, reason) from reason  # type: ignore[arg-type]
urllib3.exceptions.MaxRetryError: HTTPConnectionPool(host='localhost', port=12328): Max retries exceeded with url: /session/2fae43639c8c634131a5b00a2c7f331f/element (Caused by NewConnectionError('<urllib3.connection.HTTPConnection object at 0x000001D75F93EB90>: Failed to establish a new connection: [WinError 10061] No connection could be made because the target machine actively refused it'))

Despite these steps, the error persists. The test worked a few days ago, and I'm not sure what changed.

Any help or suggestions would be greatly appreciated.

What I have tried:

  • Confirmed that localhost:8080 is accessible and the page opens successfully.
  • Tried increasing the timeout duration.
  • Ensured both Chrome and ChromeDriver are up to date (version 126).
  • Checked that the WebDriver is correctly added to the system PATH.
  • Verified that the element #logo a is available in the DevTools and accessible.
  • Ran another script with pytest. It works perfectly:
# test_opencart.py
# there's a big conftest for this script, thought it's not important to post it here 

import pytest

@pytest.mark.parametrize("browser_name", ["chrome", "firefox", "ie"])
def test_opencart_homepage(browser_name, browser, base_url):
    browser.get(base_url)
    assert "Your Store" in browser.title, "The page title does not indicate that we are on the Opencart homepage"

@pytest.fixture
def browser_name(request):
    return request.param
2
  • Also when I tried webdriver_manager.chrome, i've got this: test_opencart_short.py::test_logo_presence PASSED [100%]An error occurred: HTTPConnectionPool(host='localhost', port=2277): Max retries exceeded with url: /session/abd36290fac92d17780b8c5cc544b9dd/element (Caused by NewConnectionError('<urllib3.connection.HTTPConnection object at 0x000001585407BA30>: Failed to establish a new connection: [WinError 10061] Подключение не установлено, т.к. конечный компьютер отверг запрос на подключение')) Commented Jul 28, 2024 at 16:43
  • By debugging I found that it is probably problem with selenium webdriver, as it uses random ports of it's own server every time. I catch this error: "Failed to establish a new connection: [WinError 10061]" Commented Jul 28, 2024 at 18:23

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.