53 lines
1.8 KiB
Python
53 lines
1.8 KiB
Python
"""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 config import config
|
|
from crawler.driver import human_delay, make_driver
|
|
|
|
|
|
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, timeout: int = 30) -> None:
|
|
self.driver = driver
|
|
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) {}"
|
|
)
|
|
|
|
self.driver.get(config.target_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,
|
|
}
|