diamond-in commited on
Commit
c2a0f3c
·
verified ·
1 Parent(s): 4424752

Update browser/driver.py

Browse files
Files changed (1) hide show
  1. browser/driver.py +107 -0
browser/driver.py CHANGED
@@ -0,0 +1,107 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """
2
+ WebDriver management and initialization
3
+ """
4
+ import os
5
+ import logging
6
+ from selenium import webdriver
7
+ from selenium.webdriver.chrome.service import Service
8
+ from selenium.webdriver.chrome.options import Options
9
+ from webdriver_manager.chrome import ChromeDriverManager
10
+ from webdriver_manager.core.os_manager import ChromeType
11
+ from config.settings import CHROMIUM_PATHS, CHROME_OPTIONS, DEFAULT_PAGE_LOAD_TIMEOUT, DEFAULT_IMPLICIT_WAIT
12
+
13
+ logger = logging.getLogger(__name__)
14
+
15
+ # Global browser session for persistence
16
+ _driver = None
17
+
18
+ def create_driver(persistent=False):
19
+ """Create Chrome driver with proper configuration"""
20
+ global _driver
21
+
22
+ # If persistent and driver exists, return it
23
+ if persistent and _driver:
24
+ try:
25
+ # Check if driver is still alive
26
+ _driver.title
27
+ return _driver
28
+ except:
29
+ # Driver is dead, create new one
30
+ _driver = None
31
+
32
+ try:
33
+ chrome_options = Options()
34
+
35
+ # Detect Chromium binary location
36
+ chromium_binary = None
37
+ for path in CHROMIUM_PATHS:
38
+ if os.path.exists(path):
39
+ chromium_binary = path
40
+ logger.info(f"Found Chromium at: {path}")
41
+ break
42
+
43
+ if chromium_binary:
44
+ chrome_options.binary_location = chromium_binary
45
+
46
+ # Add all Chrome options
47
+ for option in CHROME_OPTIONS:
48
+ chrome_options.add_argument(option)
49
+
50
+ # Try multiple driver installation methods
51
+ try:
52
+ # Method 1: Use system chromium-driver if available
53
+ if os.path.exists("/usr/bin/chromedriver"):
54
+ service = Service("/usr/bin/chromedriver")
55
+ logger.info("Using system chromedriver")
56
+ else:
57
+ # Method 2: Use webdriver-manager with ChromeType.CHROMIUM
58
+ service = Service(ChromeDriverManager(chrome_type=ChromeType.CHROMIUM).install())
59
+ logger.info("Using webdriver-manager chromedriver")
60
+ except Exception as e:
61
+ logger.warning(f"ChromeType.CHROMIUM failed: {e}")
62
+ # Method 3: Fallback to regular Chrome driver
63
+ service = Service(ChromeDriverManager().install())
64
+ logger.info("Using fallback Chrome driver")
65
+
66
+ driver = webdriver.Chrome(service=service, options=chrome_options)
67
+ driver.set_page_load_timeout(DEFAULT_PAGE_LOAD_TIMEOUT)
68
+ driver.implicitly_wait(DEFAULT_IMPLICIT_WAIT)
69
+
70
+ if persistent:
71
+ _driver = driver
72
+
73
+ return driver
74
+
75
+ except Exception as e:
76
+ logger.error(f"Failed to create driver: {e}")
77
+ raise
78
+
79
+ def close_persistent_driver():
80
+ """Close the persistent driver if it exists"""
81
+ global _driver
82
+ if _driver:
83
+ try:
84
+ _driver.quit()
85
+ except:
86
+ pass
87
+ _driver = None
88
+ return "Persistent browser closed"
89
+ return "No persistent browser to close"
90
+
91
+ def get_driver(url=None, use_persistent=False):
92
+ """Get driver and optionally navigate to URL"""
93
+ driver = create_driver(persistent=use_persistent)
94
+
95
+ if url and (not use_persistent or driver.current_url != url):
96
+ logger.info(f"Navigating to: {url}")
97
+ driver.get(url)
98
+
99
+ return driver
100
+
101
+ def cleanup_driver(driver, use_persistent=False):
102
+ """Cleanup driver if not persistent"""
103
+ if not use_persistent and driver:
104
+ try:
105
+ driver.quit()
106
+ except:
107
+ pass