@@ -19,8 +19,9 @@ def capture_page(url: str, output_file: str = "screenshot.png"):
19
19
:param output_file: The filename to save the screenshot.
20
20
"""
21
21
options = Options ()
22
+
22
23
# Basic options
23
- options .add_argument ('--headless' )
24
+ options .add_argument ('--headless=new' ) # New headless mode
24
25
options .add_argument ('--no-sandbox' ) # Required in Docker
25
26
options .add_argument ('--disable-dev-shm-usage' ) # Required in Docker
26
27
@@ -32,16 +33,24 @@ def capture_page(url: str, output_file: str = "screenshot.png"):
32
33
33
34
# Resource configuration
34
35
options .add_argument ('--window-size=1920,1080' )
35
- options .add_argument ('--remote-debugging-port=9222' ) # Fix DevTools port issue
36
- options .add_argument ('--disable-features=site-per-process' ) # Reduce memory usage
37
- options .add_argument ('--memory-pressure-off' ) # Prevent memory-related crashes
36
+ options .add_argument ('--disable-features=NetworkService,NetworkServiceInProcess' )
37
+ options .add_argument ('--disable-features=site-per-process' )
38
+
39
+ # Memory and process settings
40
+ options .add_argument ('--single-process' ) # Run in single process mode
41
+ options .add_argument ('--memory-pressure-off' )
42
+ options .add_argument ('--disable-crash-reporter' )
43
+ options .add_argument ('--disable-breakpad' ) # Disable crash reporting
38
44
39
45
# Additional stability options
40
46
options .add_argument ('--ignore-certificate-errors' )
41
- options .add_argument ('--allow-insecure-localhost' )
42
47
options .add_argument ('--disable-setuid-sandbox' )
43
48
options .add_argument ('--disable-web-security' )
44
49
50
+ # Set specific shared memory /dev/shm size (if needed)
51
+ options .add_argument ('--disable-dev-shm-usage' )
52
+ options .add_argument ('--shm-size=2g' )
53
+
45
54
# Set up Chrome service with explicit path to chromedriver and logging
46
55
service = Service (
47
56
executable_path = '/usr/local/bin/chromedriver' ,
@@ -83,7 +92,18 @@ def capture_page(url: str, output_file: str = "screenshot.png"):
83
92
raise
84
93
finally :
85
94
print ("Closing Chrome..." )
86
- driver .quit ()
95
+ try :
96
+ driver .close () # Close current window
97
+ driver .quit () # Quit browser completely
98
+ import psutil # For process cleanup
99
+ current_pid = os .getpid ()
100
+ current_process = psutil .Process (current_pid )
101
+ children = current_process .children (recursive = True )
102
+ for child in children :
103
+ if 'chrome' in child .name ().lower ():
104
+ child .terminate ()
105
+ except Exception as cleanup_error :
106
+ print (f"Error during cleanup: { cleanup_error } " )
87
107
88
108
except Exception as e :
89
109
print (f"Error initializing Chrome: { str (e )} " )
0 commit comments