Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 2 additions & 14 deletions freework_scraper/scraper/browser.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,3 @@
"""Cross-platform Selenium browser manager for FreeWork scraper."""

from __future__ import annotations

import logging
import platform

Expand All @@ -14,16 +10,12 @@

logger = logging.getLogger(__name__)


class BrowserManager:
"""Manages Selenium Chrome browser lifecycle — Windows, Mac & Linux."""

def __init__(self, headless: bool = True):
self.headless = headless
self.driver: webdriver.Chrome | None = None

def start(self) -> webdriver.Chrome:
"""Start Chrome browser with optimal settings."""
options = self._build_options()
service = Service(ChromeDriverManager().install())

Expand All @@ -37,7 +29,6 @@ def start(self) -> webdriver.Chrome:
return self.driver

def _build_options(self) -> Options:
"""Build Chrome options for cross-platform compatibility."""
options = Options()

# Core stability flags
Expand Down Expand Up @@ -76,20 +67,17 @@ def _build_options(self) -> Options:
return options

def get(self, url: str) -> None:
"""Navigate to a URL."""
if not self.driver:
raise RuntimeError("Browser not started. Call start() first.")
self.driver.get(url)

@property
def page_source(self) -> str:
"""Get current page HTML source."""
def page_source(self) -> str:
if not self.driver:
raise RuntimeError("Browser not started.")
return self.driver.page_source

def quit(self) -> None:
"""Safely close the browser."""
if self.driver:
try:
self.driver.quit()
Expand All @@ -104,4 +92,4 @@ def __enter__(self) -> BrowserManager:
return self

def __exit__(self, *args) -> None:
self.quit()
self.quit()