I'm using Flask to run a local web server and using Selenium with Chrome in headless mode to automate a task via Python.
However, when I run the Selenium script in headless mode, it times out when trying to connect to the local Flask server. I get a timeout error during page loading or element wait.
Interestingly, if I first run the same script in headed mode (i.e., without the --headless flag), it works fine. And once it works in headed mode, running it again in headless mode also works — as if the first headed execution somehow "unlocks" the headless behavior.
My Environment:
Selenium Version : 4.28.1
Python Version : 3.10.11
Python script module import list:
from selenium import webdriver
from selenium.webdriver.support.ui import Select
from selenium.webdrivermon.alert import Alert
from selenium.webdrivermon.keys import Keys
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdrivermon.action_chains import ActionChains
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdrivermon.by import By
from selenium.webdriver.remote.webelement import WebElement
from seleniummon.exceptions import NoAlertPresentException
from selenium.webdriver.chrome.options import Options
from fake_useragent import UserAgent
Python driver Option cmd:
op = webdriver.ChromeOptions()
op.add_argument("start-fullscreen")
op.add_argument("window-size=1920x1080")
op.add_argument('headless=new')
My question:
Why does headless mode fail on the first run but work after using headed mode once?
Is this related to Chrome caching, user data directories, or network behavior?
What can I do to ensure that headless mode works reliably from the first run?