Spaces:
Sleeping
Sleeping
| """ | |
| WebDriver management and initialization | |
| """ | |
| import os | |
| import logging | |
| from selenium import webdriver | |
| from selenium.webdriver.chrome.service import Service | |
| from selenium.webdriver.chrome.options import Options | |
| from webdriver_manager.chrome import ChromeDriverManager | |
| from webdriver_manager.core.os_manager import ChromeType | |
| from config.settings import CHROMIUM_PATHS, CHROME_OPTIONS, DEFAULT_PAGE_LOAD_TIMEOUT, DEFAULT_IMPLICIT_WAIT | |
| logger = logging.getLogger(__name__) | |
| # Global browser session for persistence | |
| _driver = None | |
| def create_driver(persistent=False): | |
| """Create Chrome driver with proper configuration""" | |
| global _driver | |
| # If persistent and driver exists, return it | |
| if persistent and _driver: | |
| try: | |
| # Check if driver is still alive | |
| _driver.title | |
| return _driver | |
| except: | |
| # Driver is dead, create new one | |
| _driver = None | |
| try: | |
| chrome_options = Options() | |
| # Detect Chromium binary location | |
| chromium_binary = None | |
| for path in CHROMIUM_PATHS: | |
| if os.path.exists(path): | |
| chromium_binary = path | |
| logger.info(f"Found Chromium at: {path}") | |
| break | |
| if chromium_binary: | |
| chrome_options.binary_location = chromium_binary | |
| # Add all Chrome options | |
| for option in CHROME_OPTIONS: | |
| chrome_options.add_argument(option) | |
| # Try multiple driver installation methods | |
| try: | |
| # Method 1: Use system chromium-driver if available | |
| if os.path.exists("/usr/bin/chromedriver"): | |
| service = Service("/usr/bin/chromedriver") | |
| logger.info("Using system chromedriver") | |
| else: | |
| # Method 2: Use webdriver-manager with ChromeType.CHROMIUM | |
| service = Service(ChromeDriverManager(chrome_type=ChromeType.CHROMIUM).install()) | |
| logger.info("Using webdriver-manager chromedriver") | |
| except Exception as e: | |
| logger.warning(f"ChromeType.CHROMIUM failed: {e}") | |
| # Method 3: Fallback to regular Chrome driver | |
| service = Service(ChromeDriverManager().install()) | |
| logger.info("Using fallback Chrome driver") | |
| driver = webdriver.Chrome(service=service, options=chrome_options) | |
| driver.set_page_load_timeout(DEFAULT_PAGE_LOAD_TIMEOUT) | |
| driver.implicitly_wait(DEFAULT_IMPLICIT_WAIT) | |
| if persistent: | |
| _driver = driver | |
| return driver | |
| except Exception as e: | |
| logger.error(f"Failed to create driver: {e}") | |
| raise | |
| def close_persistent_driver(): | |
| """Close the persistent driver if it exists""" | |
| global _driver | |
| if _driver: | |
| try: | |
| _driver.quit() | |
| except: | |
| pass | |
| _driver = None | |
| return "Persistent browser closed" | |
| return "No persistent browser to close" | |
| def get_driver(url=None, use_persistent=False): | |
| """Get driver and optionally navigate to URL""" | |
| driver = create_driver(persistent=use_persistent) | |
| if url and (not use_persistent or driver.current_url != url): | |
| logger.info(f"Navigating to: {url}") | |
| driver.get(url) | |
| return driver | |
| def cleanup_driver(driver, use_persistent=False): | |
| """Cleanup driver if not persistent""" | |
| if not use_persistent and driver: | |
| try: | |
| driver.quit() | |
| except: | |
| pass |