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