"""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.webdriver.support.ui import WebDriverWait from crawler.driver import human_delay 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": []} self.driver.get(self.url) human_delay(2.0, 4.0) # Wait for the page to reach a ready state self.wait.until( lambda d: d.execute_script("return document.readyState") == "complete" ) 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, }