"""Scenario 2 — fresh cookie jar per run so the site treats the visitor as a new user.""" from __future__ import annotations import undetected_chromedriver as uc from selenium.common.exceptions import TimeoutException from selenium.webdriver.support.ui import WebDriverWait from crawler.driver import human_delay from logger import get_logger log = get_logger(__name__) class FreshSessionScenario: """ Opens the target URL in a brand-new browser profile (no stored cookies, localStorage, or cache) so each run appears as a first-time visitor. Because undetected-chromedriver already creates an isolated temp profile per instance, simply spinning up a new driver is sufficient — but we also explicitly delete all cookies after loading to be safe. """ def __init__(self, driver: uc.Chrome, url: str, timeout: int = 30) -> None: self.driver = driver self.url = url self.wait = WebDriverWait(driver, timeout) def run(self) -> dict[str, object]: """Navigate to the target, clear any residual state, return page info.""" self.driver.delete_all_cookies() # Clear localStorage / sessionStorage via JS self.driver.execute_script( "try { window.localStorage.clear(); window.sessionStorage.clear(); } catch(e) {}" ) if not self.url: return {"title": "", "url": "", "cookie_count": 0, "cookies": []} try: self.driver.get(self.url) except TimeoutException: log.warning( "Page load exceeded the browser timeout; stopping navigation and continuing" ) self.driver.execute_cdp_cmd("Page.stopLoading", {}) human_delay(2.0, 4.0) # Interactive is sufficient for the dynamic flow and avoids waiting on # analytics, ads, or other background resources indefinitely. try: self.wait.until( lambda d: d.execute_script("return document.readyState") in ("interactive", "complete") ) except TimeoutException: log.warning("Document did not report a ready state; continuing with the current DOM") title = self.driver.title current_url = self.driver.current_url cookies = self.driver.get_cookies() return { "title": title, "url": current_url, "cookie_count": len(cookies), "cookies": cookies, }