4
4
from selenium import webdriver
5
5
from selenium .webdriver .chrome .options import Options
6
6
from selenium .webdriver .chrome .service import Service
7
+ from subprocess import PIPE , STDOUT
7
8
print ("Gradio app loaded." )
8
9
9
10
def capture_page (url : str , output_file : str = "screenshot.png" ):
@@ -14,15 +15,31 @@ def capture_page(url: str, output_file: str = "screenshot.png"):
14
15
:param output_file: The filename to save the screenshot.
15
16
"""
16
17
options = Options ()
17
- options .add_argument ("--headless" ) # Run in headless mode
18
- options .add_argument ("--window-size=1920,1080" ) # Set window size
18
+ options .add_argument ('--headless' )
19
+ options .add_argument ('--no-sandbox' ) # Required in Docker
20
+ options .add_argument ('--disable-dev-shm-usage' ) # Required in Docker
21
+ options .add_argument ('--disable-gpu' ) # Required in Docker
22
+ options .add_argument ('--disable-software-rasterizer' )
23
+ options .add_argument ('--window-size=1920,1080' )
24
+ options .add_argument ('--disable-extensions' )
25
+ options .add_argument ('--disable-infobars' )
19
26
20
- # Initialize Chrome service
21
- service = Service ()
22
- driver = webdriver .Chrome (service = service , options = options )
27
+ # Set up Chrome service with explicit path to chromedriver and logging
28
+ service = Service (
29
+ executable_path = '/usr/local/bin/chromedriver' ,
30
+ log_output = PIPE # Redirect logs to pipe
31
+ )
32
+
33
+ # Initialize Chrome with the service and options
34
+ driver = webdriver .Chrome (
35
+ service = service ,
36
+ options = options
37
+ )
23
38
24
39
try :
25
40
driver .get (url )
41
+ # Add a small delay to ensure page loads completely
42
+ driver .implicitly_wait (5 )
26
43
driver .save_screenshot (output_file )
27
44
print (f"Screenshot saved: { output_file } " )
28
45
finally :
@@ -44,6 +61,7 @@ def capture_and_show(url: str):
44
61
# Return the image path
45
62
return temp_path
46
63
except Exception as e :
64
+ print (f"Error in capture_and_show: { str (e )} " ) # Add detailed logging
47
65
return f"Error capturing page: { str (e )} "
48
66
49
67
def create_gradio_app ():
0 commit comments