Open target URL during Chrome startup

This commit is contained in:
2026-08-03 00:42:09 +03:30
parent 9f986255b3
commit 71790cf0ad
5 changed files with 71 additions and 13 deletions
+44 -4
View File
@@ -13,9 +13,10 @@ import tempfile
import time
from contextlib import contextmanager
from pathlib import Path
from typing import Generator
from typing import Any, Generator, cast
import undetected_chromedriver as uc
from selenium.common.exceptions import WebDriverException
from selenium.webdriver.chrome.options import Options
from selenium_stealth import stealth
@@ -90,10 +91,13 @@ def _build_options(
chrome_binary: str,
profile_dir: str | None = None,
display: str | None = None,
initial_url: str | None = None,
) -> Options:
opts = Options()
opts.binary_location = chrome_binary
opts.page_load_strategy = "eager"
# Never let ChromeDriver session creation block on the initial New Tab
# renderer. Scenarios perform their own explicit URL/readiness waits.
opts.page_load_strategy = "none"
if headless:
opts.add_argument("--headless=new")
if profile_dir:
@@ -108,6 +112,10 @@ def _build_options(
opts.add_argument("--disable-blink-features=AutomationControlled")
opts.add_argument("--disable-infobars")
opts.add_argument("--window-size=1920,1080")
if initial_url and initial_url.startswith(("http://", "https://")):
# Opening the configured page as a Chrome process argument avoids
# waiting for the initial New Tab renderer before navigation begins.
opts.add_argument(initial_url)
return opts
@@ -115,14 +123,27 @@ def make_driver(
headless: bool | None = None,
profile_dir: str | None = None,
display: str | None = None,
initial_url: str | None = None,
) -> uc.Chrome:
"""Return a stealthed undetected Chrome instance."""
use_headless = config.headless if headless is None else headless
chrome_binary, major = _detect_chrome()
ua = random.choice(_USER_AGENTS)
opts = _build_options(ua, use_headless, chrome_binary, profile_dir, display)
opts = _build_options(
ua,
use_headless,
chrome_binary,
profile_dir,
display,
initial_url,
)
log.debug("Starting ChromeDriver (headless=%s, Chrome %d)", use_headless, major)
log.info(
"Starting ChromeDriver (headless=%s, Chrome %d, initial_url=%s)",
use_headless,
major,
initial_url or "<none>",
)
driver = uc.Chrome(
options=opts,
@@ -130,6 +151,23 @@ def make_driver(
version_main=major,
use_subprocess=True,
)
log.info("ChromeDriver connected")
if initial_url and initial_url.startswith(("http://", "https://")):
navigation = cast(
dict[str, Any],
driver.execute_cdp_cmd( # type: ignore[reportUnknownMemberType]
"Page.navigate", {"url": initial_url}
),
)
error_text = navigation.get("errorText")
if error_text:
raise WebDriverException(
f"Chrome rejected initial navigation to {initial_url}: {error_text}"
)
log.info("Initial navigation dispatched to %s", initial_url)
log.info("Applying stealth settings")
if display:
driver.set_window_size(1920, 1080)
@@ -191,6 +229,7 @@ def _terminate_profile_processes(profile_dir: str) -> None:
def driver_session(
headless: bool | None = None,
recording_name: str | None = None,
initial_url: str | None = None,
) -> Generator[uc.Chrome, None, None]:
profile_dir = tempfile.mkdtemp(prefix="seed-chrome-")
driver: uc.Chrome | None = None
@@ -207,6 +246,7 @@ def driver_session(
effective_headless,
profile_dir=profile_dir,
display=display,
initial_url=initial_url,
)
yield driver
finally: