最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

python 3.x - Selenium Chrome headless mode times out on first run, but works fine after running in headed mode once – why - Stac

programmeradmin9浏览0评论

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:

  1. Why does headless mode fail on the first run but work after using headed mode once?

  2. Is this related to Chrome caching, user data directories, or network behavior?

  3. What can I do to ensure that headless mode works reliably from the first run?

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论